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);
        }```


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM