EasyExcel 复杂表头的导出(动态表头和静态表头)

问题:如图,1部分的表头是动态的根据日期变化,2部分是数据库对应的字段,静态不变的;

解决方案:如果不看1的部分,2部分+内容可以根据实体类+注解的方式导出,那么我们是不是可以先将动态表头1 写入到Excel中,然后再用注解+实体类的方式将2部分和内容写入到Excel中。

// 以下是伪代码参考,无法直接运行

// 设置动态表头
List<String> head = new ArrayList<>();

String name = String.format(sheetName, LocalDate.now().getYear(), LocalDate.now().getMonth().getValue());

if (StringUtils.isNotBlank(publicityReq.getCountyId())) {
    name = name + "\n" + depart + departName;
}

head.add(name);
headList.add(head);

ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), TPublicityInfo.class).build();
// 创建sheet
WriteSheet writeSheet = EasyExcel.writerSheet(String.format(sheetName, LocalDate.now().getYear(), LocalDate.now().getMonth().getValue())).needHead(Boolean.FALSE).build();
// 将动态表头写入sheet,new ArrayList<>()代表写入空数据
WriteTable writeTable0 = EasyExcel.writerTable(0).head(headList).registerWriteHandler(new OnceAbsoluteMergeStrategy(0, 2, 0, 7)).needHead(Boolean.TRUE).build();

excelWriter.write(new ArrayList<>(), writeSheet, writeTable0);
// 将静态表头+内容写入sheet,tPublicityInfos代表内容数据
WriteTable writeTable2 = EasyExcel.writerTable(3).head(TPublicityInfo.class).relativeHeadRowIndex(2).needHead(Boolean.TRUE).build();

excelWriter.write(tPublicityInfos, writeSheet, writeTable2);
excelWriter.finish();

下边是项目中的完整代码,可以做简单参考

public void exportExcel(Principal principal, HttpServletResponse response, PublicityInfoReq publicityReq) throws IOException {
    String sheetName = "%s年%s月xxxx";
    String depart = "单位:";
    String personId = AuthenticationInfo.getUserId(principal);
    String departsCode = "";
    if (StringUtils.isNotEmpty(publicityReq.getCorpId())) {
        DepartInfo departInfo = iDepartSCodeService.getVDeptById(publicityReq.getCorpId());
        departsCode = departInfo.getDepartScode();
    } else {
        departsCode = iDepartSCodeService.getDeptScode(personId);
    }
    publicityReq.setDepartsCode(departsCode);

    List<TPublicityInfo> tPublicityInfos = publicityMapper.listByPublicity(publicityReq);

    if (CollectionUtils.isNotEmpty(tPublicityInfos)) {
        for (int i = 0; i < tPublicityInfos.size(); i++) {
            TPublicityInfo tPublicityInfo = tPublicityInfos.get(i);
            tPublicityInfo.setNum(i+1);
        }
    }

    List<String> departNames = this.baseMapper.selectDepartNameByDepartId(publicityReq.getCountyId());

    String departName = "";
    if (CollectionUtils.isNotEmpty(departNames) && departNames.size() == 1) {
        departName = departNames.get(0);
    }
    List<List<String>> headList = new ArrayList<>();
    List<String> head = new ArrayList<>();

    String name = String.format(sheetName, LocalDate.now().getYear(), LocalDate.now().getMonth().getValue());

    if (StringUtils.isNotBlank(publicityReq.getCountyId())) {
        name = name + "\n" + depart + departName;
    }

    head.add(name);
    headList.add(head);

    ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), TPublicityInfo.class).build();

    WriteSheet writeSheet = EasyExcel.writerSheet(String.format(sheetName, LocalDate.now().getYear(), LocalDate.now().getMonth().getValue())).needHead(Boolean.FALSE).build();

    WriteTable writeTable0 = EasyExcel.writerTable(0).head(headList).registerWriteHandler(new OnceAbsoluteMergeStrategy(0, 2, 0, 7)).needHead(Boolean.TRUE).build();

    excelWriter.write(new ArrayList<>(), writeSheet, writeTable0);

    WriteTable writeTable2 = EasyExcel.writerTable(3).head(TPublicityInfo.class).relativeHeadRowIndex(2).needHead(Boolean.TRUE).build();
    excelWriter.write(tPublicityInfos, writeSheet, writeTable2);
    excelWriter.finish();
}

本文由博客一文多发平台 OpenWrite 发布!

相关推荐
王ASC8 小时前
SpringMVC的URL组成,以及URI中对/斜杠的处理,解决IllegalStateException: Ambiguous mapping
java·mvc·springboot·web
撒呼呼8 小时前
# 起步专用 - 哔哩哔哩全模块超还原设计!(内含接口文档、数据库设计)
数据库·spring boot·spring·mvc·springboot
灰色孤星A15 小时前
瑞吉外卖项目学习笔记(四)@TableField(fill = FieldFill.INSERT)公共字段填充、启用/禁用/修改员工信息
java·学习笔记·springboot·瑞吉外卖·黑马程序员·tablefield·公共字段填充
武子康2 天前
Java-31 深入浅出 Spring - IoC 基础 启动IoC XML与注解结合的方式 配置改造 applicationContext.xml
java·大数据·spring·mybatis·springboot
synda@hzy3 天前
MONI后台管理系统-系统三员的设计
java·springboot·等级保护·三员管理
灰色孤星A3 天前
瑞吉外卖项目学习笔记(二)Swagger、logback、表单校验和参数打印功能的实现
springboot·logback·swagger·瑞吉外卖·切面编程·表单校验·黑马程序员
langzitianya3 天前
RestTemplate实时接收Chunked编码传输的HTTP Response
springboot·stream·resttemplate·chunked·流式
武子康3 天前
Java-30 深入浅出 Spring - IoC 基础 启动IoC 纯XML启动 Bean、DI注入
xml·java·开发语言·后端·spring·mybatis·springboot
Moshow郑锴4 天前
Spring Boot中CollectionUtils怎么用
springboot·数组·collectionutil
武子康4 天前
大数据-253 离线数仓 - Airflow 任务调度 核心概念与实际案例测试 Py脚本编写
java·大数据·数据仓库·hive·hadoop·springboot