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

相关推荐
武子康6 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
初晴~20 小时前
【Spring】RESTful设计风格
java·后端·spring·springboot·restful
阑梦清川1 天前
SpringMVC案例学习(二)--表白墙/图书管理系统1.0版本
spring·mvc·springboot·案例
武子康2 天前
Java-04 深入浅出 MyBatis - SqlSessionFactory 与 SqlSession DAO与Mapper 代理模式
java·mysql·spring·mybatis·springboot·代理模式
进击的阿晨3 天前
Kotlin:后端开发的新宠
后端·springboot
VipSoft3 天前
RedisTemplate RedisConfig 序列化方式 fastjson2
springboot
VipSoft4 天前
MinIO Linux 安装使用 & SpringBoot整合MinIO
springboot
阿华的代码王国5 天前
【Bug合集】——Java大小写引起传参失败,获取值为null的解决方案
java·开发语言·springboot
52 IT5 天前
解决若依ruoyi项目部署到服务器验证码接口报错的问题
springboot·ruoyi
武子康5 天前
Java-01 深入浅出 MyBatis - MyBatis 概念 ORM映射关系 常见ORM 详细发展历史
java·数据库·sql·spring·mybatis·springboot·springcloud