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 发布!

相关推荐
全栈凯哥10 小时前
CommandLineRunner详细教程
springboot
Thanks_ks2 天前
SpringBoot 自动化部署实战:CI/CD 整合方案与避坑指南
pipeline·jenkins·springboot·自动化部署·gitlab ci/cd·ci/cd 实战·docker 容器化
保持学习ing5 天前
黑马Java面试笔记之框架篇(Spring、SpringMvc、Springboot)
java·笔记·spring·面试·mvc·mybatis·springboot
怡人蝶梦6 天前
Java大厂后端技术栈故障排查实战:Spring Boot、Redis、Kafka、JVM典型问题与解决方案
java·jvm·redis·elk·kafka·springboot·prometheus
怡人蝶梦7 天前
Java后端技术栈问题排查实战:Spring Boot启动慢、Redis缓存击穿与Kafka消费堆积
java·jvm·redis·kafka·springboot·prometheus
怡人蝶梦8 天前
Spring Boot启动慢?Redis缓存击穿?Kafka消费堆积?——Java后端常见问题排查实战
java·jvm·redis·kafka·springboot·prometheus·microservices
JAVA坚守者8 天前
Java 对接 Office 365 邮箱全攻略:OAuth2 认证 + JDK8 兼容 + Spring Boot 集成(2025 版)
springboot·oauth2·office365·java 开发·企业级开发·jdk8 兼容
黎黎黎明⁠⁢9 天前
SpringBoot整合Sa-Token:实现RBAC权限模型
java·sa-token·springboot·idea
计算机软件bs辅导9 天前
t009-线上代驾管理系统
毕业设计·软件工程·springboot·大学生·计算机软件·毕设辅导·代驾系统