easyexcel往已存在的excel文件里追加数据

        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);
        }```
posted @ 2021-12-16 21:32  迷茫的小草  阅读(9061)  评论(0编辑  收藏  举报