ExcelWriter excelWriter = null; 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); }
easyexcel已存在的excel里追加数据
最新推荐文章于 2024-04-12 16:45:09 发布