Java Excel文件内容替换

/**
     * 替换Excel模板文件内容
     *
     * @param map     需要替换的标签建筑队形式
     * @param intPath Excel模板文件路径
     * @param outPath Excel生成文件路径
     */
    public static void replaceSheetsModel(Map map,String intPath,String outPath) {
        try {
            FileInputStream fs = new FileInputStream(intPath);
            XSSFWorkbook workbook = new XSSFWorkbook(fs);
            XSSFWorkbook wb = (XSSFWorkbook) workbook;
            XSSFSheet sheet;
            for (int j = 0; j < wb.getNumberOfSheets(); j++) {
                sheet = workbook.getSheetAt(j);
                Iterator rows = sheet.rowIterator();
                while (rows.hasNext()) {
                    XSSFRow row = (XSSFRow) rows.next();
                    if (row != null) {
                        int num = row.getLastCellNum();
                        for (int i = 0; i < num; i++) {
                            XSSFCell cell = row.getCell(i);
                            if (cell != null) {
                                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                            }
                            if (cell == null || cell.getStringCellValue() == null) {
                                continue;
                            }
                            String value = cell.getStringCellValue();
                            if (!"".equals(value)) {
                                Set keySet = map.keySet();
                                Iterator it = keySet.iterator();
                                while (it.hasNext()) {
                                    String text = it.next();
                                    if (value.equalsIgnoreCase(text)) {
                                        cell.setCellValue((String) map.get(text));
                                        break;
                                    }
                                }
                            } else {
                                cell.setCellValue("");
                            }
                        }
                    }
                }
            }
            FileOutputStream fileOut = new FileOutputStream(outPath);
            wb.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
 
            e.printStackTrace();
        }

    }

你可能感兴趣的:(Excel,java开发,java)