File templateFile = new File(filePath, fileName);
File destFile = new File(filePath, "test.xlsx");
try {
if (templateFile.exists()) {
//追加數據,目標文件與原始文件不能是同一個文件名
//withTemplate()指定模板文件
excelWriter = EasyExcel.write().withTemplate(templateFile)
//.file() 指定目標文件,不能與模板文件是同一個文件
.file(destFile).autoCloseStream(false).build();
} else {
excelWriter = EasyExcel.write(templateFile, ElemPersonListExcel.class)
.build();
}
WriteSheet writeSheet = EasyExcel.writerSheet("人員清單")
.build();
excelWriter.write(elemPersonListExcelList, writeSheet);
} finally {
// 千萬別忘記finish 會幫忙關閉流
if (excelWriter != null) {
excelWriter.finish();
}
}
if (destFile.exists()) {
//刪除原模板文件,新生成的文件變成新的模板文件
templateFile.delete();
destFile.renameTo(templateFile);
}```