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);
相关推荐
计算机毕设指导611 分钟前
基于 SpringBoot 的作业管理系统【附源码】
java·vue.js·spring boot·后端·mysql·spring·intellij-idea
Gu Gu Study13 分钟前
枚举与lambda表达式,枚举实现单例模式为什么是安全的,lambda表达式与函数式接口的小九九~
java·开发语言
Chris _data15 分钟前
二叉树oj题解析
java·数据结构
牙牙70521 分钟前
Centos7安装Jenkins脚本一键部署
java·servlet·jenkins
paopaokaka_luck29 分钟前
[371]基于springboot的高校实习管理系统
java·spring boot·后端
以后不吃煲仔饭41 分钟前
Java基础夯实——2.7 线程上下文切换
java·开发语言
进阶的架构师42 分钟前
2024年Java面试题及答案整理(1000+面试题附答案解析)
java·开发语言
The_Ticker1 小时前
CFD平台如何接入实时行情源
java·大数据·数据库·人工智能·算法·区块链·软件工程
大数据编程之光1 小时前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
爪哇学长1 小时前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法