JXLS 导出多sheet,带页眉页脚

java 复制代码
    /**
     * 生成多sheet Excel
     * 带自定义页眉页脚
     *
     * @param templatePath
     * @param sheetList
     * @return
     * @throws IOException
     */
    public static byte[] generateMultiSheet(String templatePath, List<JxlsHelper2.SheetContext> sheetList) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //设置页眉页脚等布局
        try (InputStream is = new ClassPathResource(templatePath).getInputStream()) {
            XSSFWorkbook wb = new XSSFWorkbook(is);
            for (JxlsHelper2.SheetContext sheetContext : sheetList) {
                if (StringUtils.isEmpty(sheetContext.getName())) {
                    continue;
                }
                XSSFSheet sheet = wb.getSheet(sheetContext.getName());
                if (sheet != null) {
                    translateHeaderTemplate(sheet, sheetContext.getContext());
                }
            }
            wb.write(baos);
            //渲染数据
            ByteArrayOutputStream targetStream = new ByteArrayOutputStream();
            try (InputStream templateStream = new ByteArrayInputStream(baos.toByteArray())) {
                JxlsHelper2 jxlsHelper = JxlsHelper2.getInstance();
                Transformer transformer = jxlsHelper.createTransformer(templateStream, targetStream);
                jxlsHelper.processTemplate(sheetList, transformer);

            }

            return targetStream.toByteArray();
        }
    }
复制代码
重写 package org.jxls.util.JxlsHelper;
java 复制代码
    /**
     * jx:area(lastCell="R3" multisheet ="sheets")
     *
     * @param sheetList
     * @param transformer
     * @throws IOException
     */
    public void processTemplate(List<SheetContext> sheetList, Transformer transformer) throws IOException {
        areaBuilder.setTransformer(transformer);
        List<Area> xlsAreaList = areaBuilder.build();
        for (Area xlsArea : xlsAreaList) {
            String sheetName = xlsArea.getStartCellRef().getSheetName();
            Optional<SheetContext> optional = sheetList.stream().filter(s -> Objects.equals(sheetName, s.getName())).findAny();
            if (optional.isPresent()) {
                xlsArea.applyAt(new CellRef(xlsArea.getStartCellRef().getCellName()), optional.get().getContext());
            }
        }
        if (processFormulas) {
            for (Area xlsArea : xlsAreaList) {
                setFormulaProcessor(xlsArea);
                xlsArea.processFormulas();
            }
        }
        if (isHideTemplateSheet()) {
            List<String> sheetNameTemplate = getSheetsNameOfMultiSheetTemplate(xlsAreaList);
            for (String sheetName : sheetNameTemplate) {
                transformer.setHidden(sheetName, true);
            }
        }
        if (isDeleteTemplateSheet()) {
            List<String> sheetNameTemplate = getSheetsNameOfMultiSheetTemplate(xlsAreaList);
            for (String sheetName : sheetNameTemplate) {
                transformer.deleteSheet(sheetName);
            }
        }
        transformer.write();
    }

增加DTO

java 复制代码
    @Data
    @AllArgsConstructor
    public static class SheetContext {
        /**
         * sheet name
         */
        private String name;
        private Context context;
    }

调用:

java 复制代码
            List<JxlsHelper2.SheetContext> sheetList = new ArrayList<>();
            Context context = new Context();
            for (String key : contextMap.keySet()) {
                context.putVar(key, contextMap.get(key));
            }
            sheetList.add(new JxlsHelper2.SheetContext("Sheet1", context));

            //
            Map<String, Object> contextMap2 = defaultExcelReportProvider.getContextMap("7777", "333", "测试", data);
            Context context2 = new Context();
            for (String key : contextMap2.keySet()) {
                context2.putVar(key, contextMap2.get(key));
            }
            sheetList.add(new JxlsHelper2.SheetContext("Sheet2", context2));
            byte[] bytes = JxlsUtil.generateMultiSheet(getExcelTemplatePath(), sheetList);
相关推荐
小鑫记得努力8 分钟前
Java类和对象(下篇)
java
binishuaio12 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE13 分钟前
【Java SE】StringBuffer
java·开发语言
老友@14 分钟前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
wrx繁星点点29 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui32 分钟前
Aop+自定义注解实现数据字典映射
java
zzzgd81632 分钟前
easyexcel实现自定义的策略类, 最后追加错误提示列, 自适应列宽,自动合并重复单元格, 美化表头
java·excel·表格·easyexcel·导入导出
友善的鸡蛋33 分钟前
解决:使用EasyExcel导入Excel模板时出现数据导入不进去的问题
java·easyexcel·excel导入
星沁城33 分钟前
240. 搜索二维矩阵 II
java·线性代数·算法·leetcode·矩阵
NoneCoder1 小时前
Java企业级开发系列(1)
java·开发语言·spring·团队开发·开发