Apache POI批量修改Excel文件
我正在运行一个修改大型Excel文件的Java应用程序。 而且我正在经历更新发生的时间。
对于每个单元,我正在运行基于更改的更新作为休闲
public boolean updateCellData(ColumnName, RowNum, Data){ FileInputStream fis = new FileInputStream(path); Workbook workbook = WorkbookFactory.create(fis); row = getRow(rowNum-1); if (row == null){ row = sheet.createRow(rowNum-1); } cell = row.getCell(colNum); if (cell == null){ cell = row.createCell(colNum); } cell.setCellValue(data); fileOut = new FileOutputStream(path); workbook.write(fileOut); fileOut.close(); }
有什么优化,我可以做我的代码?
打开excel一次,毕竟你的更新操作closures了excel。
例如:
FileInputStream fis =null; Workbook workbook=null; public void openWorkbook(){ fis = new FileInputStream(path); workbook = WorkbookFactory.create(fis); } public boolean updateCellData(ColumnName, RowNum, Data){ row = getRow(rowNum-1); if (row == null){ row = sheet.createRow(rowNum-1); } cell = row.getCell(colNum); if (cell == null){ cell = row.createCell(colNum); } cell.setCellValue(data); } public void closeWorkbook(){ fileOut = new FileOutputStream(path); workbook.write(fileOut); fileOut.close(); }