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

相关推荐
CoderJia程序员甲8 小时前
重学SpringBoot3-集成Redis(一)
java·redis·缓存·springboot
学习ing小白2 天前
JavaWeb - 8 - 请求响应 & 分层解耦
java·springboot·后端web开发
Hello Dam3 天前
【文件增量备份系统】MySQL百万量级数据量分页查询性能优化
java·mysql·性能优化·springboot·深分页优化
大灰狼19134 天前
【怎样基于Okhttp3来实现各种各样的远程调用,表单、JSON、文件、文件流等待】
java·springboot·网络流·okhttp3·文件流传输
一 乐4 天前
高校体育场小程序|高校体育场管理系统系统|体育场管理系统小程序设计与实现(源码+数据库+文档)
数据库·小程序·vue·源码·springboot·体育馆小程序
爱跑步的程序员~5 天前
若依框架使用教程
vue.js·低代码·mybatis·springboot
yicj5 天前
SpringBoot3 Swagger笔记整理
java·springboot·swagger
一 乐6 天前
畅阅读小程序|畅阅读系统|基于java的畅阅读系统小程序设计与实现(源码+数据库+文档)
java·小程序·vue·源码·springboot·阅读小程序
IT利刃出鞘6 天前
SpringBoot--为什么Controller是串行的?怎样才能并行?
springboot
catoop6 天前
SpringBoot 注解 @AutoConfiguration 在 2.7 版本中被新增(使用方法)
springboot