alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

如何实现填充多个sheet,使用同一份sheet模板 #3925

Open heiox opened 1 month ago

heiox commented 1 month ago

目前是需要在模板文件中,将sheet模板复制多个。

HDHXTM commented 3 weeks ago

找到方案了吗。我的需求和你一样,但是我的sheet数量是不确定的

        InputStream inputStream = FileUtils.getExportTemplate("部门车资.xlsx");
        ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream()).withTemplate(inputStream);
        ExcelWriter excelWriter = writerBuilder.build();
        for (String dept : deptSendCarMap.keySet()) {
            List<CarfareExcel> deptCarfareList = deptSendCarMap.get(dept);
            // 省略其他数据构建

            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(Collections.singletonMap("dept", dept), fillConfig, writeSheet);
            excelWriter.fill(deptCarfareList, fillConfig, writeSheet);
            Map<String, Object> map = new HashMap<>();
            map.put("totaltoll", totaltoll);
            map.put("cost", cost);
            map.put("invoice", invoice);
            excelWriter.fill(map, writeSheet);
        }
        excelWriter.finish();

这样写只会重复在模版的sheet里写。得到的数据只有最后一个deptCarfareList的 如果这样写:WriteSheet writeSheet = EasyExcel.writerSheet(dept).build(); 对应的sheet是创建了,但是里面没有任何数据,因为新创建的sheet里并没有模版

heiox commented 3 weeks ago

没有很好的解决方案,我是在填充之前,按照所需先复制sheet:workbook.cloneSheet

HDHXTM commented 3 weeks ago

没有很好的解决方案,我是在填充之前,按照所需先复制sheet:workbook.cloneSheet

大佬,具体怎么写的,这样也行,起码能用

heiox commented 3 weeks ago
截屏2024-09-12 14 49 20
HDHXTM commented 3 weeks ago

成功了,完整代码放这里,供后人参考

        InputStream inputStream = FileUtils.getExportTemplate("部门车资.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        List<String> deptNames = new ArrayList<>(deptSendCarMap.keySet());
        for (int i = 0; i < deptNames.size(); i++) {
            if (i == 0) {
                workbook.setSheetName(0, deptNames.get(0));
            } else {
                workbook.cloneSheet(0, deptNames.get(i));
            }
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        workbook.write(bos);
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(bis).build();
        for (int i = 0; i < deptNames.size(); i++) {
            String dept = deptNames.get(i);
            List<CarfareExcel> deptCarfareList = deptSendCarMap.get(dept);
            // 省略其他数据构建
            WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(Collections.singletonMap("dept", dept), fillConfig, writeSheet);
            excelWriter.fill(deptCarfareList, fillConfig, writeSheet);
            Map<String, Object> map = new HashMap<>();
            map.put("totaltoll", totaltoll);
            map.put("cost", cost);
            map.put("invoice", invoice);
            excelWriter.fill(map, writeSheet);
        }
        excelWriter.finish();