[英]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.