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);
相关推荐
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
Hello.Reader2 小时前
深入解析 Apache APISIX
java·apache
菠萝蚊鸭3 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪3 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0073 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生3 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss3 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm
一棵星3 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言