[英]JAVA - Apache POI batch modify Excel File
我正在运行一个修改大型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,打开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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.