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