easypoi动态导出多级表头(三级+)

使用版本4.1.0

        先说一下背景,业务需求根据客户选择,动态导出Excel,根据其数据情况,会有多级表头出现。这种情况下使用注解导出数据就不现实了,模板也不行,我这边使用的是map导出数据(后经过实验,注解导出数据也会出现以下BUG),发现的BUG。

原本4.1.0版本easypoi导出多级表头(三级表头及以上),会有两个BUG,此处以四级表头为例子

一:有多重三级表头情况下,只有最后一个三级表头会有字段名,前面的都为空

如图:

 如果你的业务需求只需要一个三级表头,则此BUG不会影响你

二:表头偏移,表头级数越多,更显著

如图:

可能有的小伙伴会说自己也是三级表头,但是没有这个BUG之类的,下面来说这两个BUG的出现原因。 

一.空字段

       此BUG在4.1.1版本(写文章时的最新版)解决了,但是4.1.1有个新BUG,所以此次修改还是基于4.1.0来处理的。

        直接看问题源码

    /**
     * 创建表头
     */
    private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index,
                                List<ExcelExportEntity> excelParams, int cellIndex) {
        Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index);
        int rows = getRowNums(excelParams, true);
        row.setHeight(title.getHeaderHeight());
        Row listRow = null;


        if (rows >= 2) {
            listRow = sheet.createRow(index + 1);
            listRow.setHeight(title.getHeaderHeight());
        }


        int groupCellLength = 0;
        CellStyle titleStyle = getExcelExportStyler().getTitleStyle(title.getColor());
}

        此处源码是节选easypoi生成表头的源码处,使用换行隔开的就是问题源码所在

        因为此BUG官方已经修复了,所以我就不多说了,直接贴上官方修复之后的源码,看一眼就懂BUG出现原因

    /**
     * 创建表头
     */
    private int createHeaderRow(ExportParams title, Sheet sheet, Workbook workbook, int index,
                                List<ExcelExportEntity> excelParams, int cellIndex) {
        Row row = sheet.getRow(index) == null ? sheet.createRow(index) : sheet.getRow(index);
        int rows = getRowNums(excelParams, true);
        row.setHeight((short)title.getHeaderHeight());
        Row listRow = null;


        if (rows >= 2) {
            listRow = sheet.getRow(index + 1);
            if (listRow == null) {
                listRow = sheet.createRow(index + 1);
                listRow.setHeight((short)
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: easypoi是一款Java的Excel导入导出工具,支持多级表头导出。在使用easypoi导出Excel时,可以通过设置表头的行数和列数来实现多级表头导出。具体实现方法可以参考easypoi的官方文档或者相关的教程。需要注意的是,在设置多级表头时,需要根据实际情况设置每个单元格的合并行数和列数,以确保表头的显示效果正确。 ### 回答2: Easypoi是一个Java的Excel处理框架,它可以方便快捷地导入和导出Excel文档。在使用Easypoi进行Excel导出时,实现多级表头的方法是非常简单的。 首先,需要在Java类中定义表头的结构。在Easypoi中,可以使用实体类的注解来定义表头,例如 @Excel(name = "学生信息", width = 20) 代表一个表头的名称为“学生信息”,宽度为20个字符。对于多级表头的情况,可以在实体类中嵌套另一个实体类来表示多级表头的结构。 接着,使用excel导出工具类进行导出。在使用Easypoi进行Excel导出时,可以使用ExcelExportUtil.exportExcel方法来实现。该方法需要传入一个Excel导出参数的包装类,其中包括Excel文档的标题、表头、表数据等信息。 对于多级表头的情况,可以通过设置ExcelExportParams的headMap参数来实现。该参数是一个Map<String, Integer>对象,其中key为表头的文本,value为该表头所占据的列数。例如,要实现一个两层表头,第一层表头为“学生信息”,占据4列;第二层表头为“基本信息”和“成绩信息”,分别占据2列,则可以定义headMap参数为: Map<String, Integer> headMap = new LinkedHashMap<>(); headMap.put("学生信息", 4); headMap.put("基本信息", 2); headMap.put("成绩信息", 2); 最后,调用ExcelExportUtil.exportExcel方法进行导出即可。完整的导出代码如下: ```java List<Student> students = new ArrayList<>(); // TODO: 设置学生列表数据 // 定义 Excel 导出参数 ExcelExportParams exportParams = new ExcelExportParams(); exportParams.setTitle("学生信息表"); exportParams.setHead(StudentExcelExportModel.class, headMap); // 导出 Excel Workbook workbook = ExcelExportUtil.exportExcel(exportParams, StudentExcelExportModel.class, students); workbook.write(outputStream); ``` 需要注意的是,ExcelExportParams的setHead方法需要传入一个Class类型的参数,用于指定表头的字段。这个Class可以和数据实体类不一致,因为其只关心表头信息。 综上所述,使用Easypoi导出Excel多级表头非常方便。只需在数据实体类中定义表头结构,然后设置Excel导出参数的headMap参数并调用ExcelExportUtil.exportExcel方法即可。 ### 回答3: Easypoi是一个开源的Java工具库,用于生成Excel、Word和Pdf等文档格式,它提供了简单易用的API,可以帮助我们快速导出Excel数据。在使用Easypoi导出Excel时,往往遇到多级表头的情况,本文将详细介绍Easypoi导出Excel多级表头的实现方法。 Easypoi支持导出Excel的表头分为两种:固定表头动态表头。因为不同类型的表头实现方式不同,所以我们需要分开讲解。 1. 固定表头导出 固定表头就是表头中包含多个级别,其中每个级别都是已经确定的,不会随数据量的增加而增加。在Easypoi中,我们可以使用@TableStyle注解中的headRows和secondHeadRows来设置多级表头的行数,具体实现步骤如下: ① 在需要导出的实体类中定义表头,例如: public class Student { @Excel(name = "学号", orderNum = "0", width = 15) private String id; @Excel(name = "姓名", orderNum = "1", width = 15) private String name; @Excel(name = "语文", orderNum = "2", width = 15) private Integer chinese; @Excel(name = "数学", orderNum = "3", width = 15) private Integer math; @Excel(name = "英语", orderNum = "4", width = 15) private Integer english; } ② 在需要导出的Controller中设置表格样式,使用@TableStyle注解,例如: @GetMapping("/exportStudent") public void exportStudent(HttpServletResponse response) throws IOException { // 模拟数据 List<Student> studentList = new ArrayList<>(); Student stu1 = new Student("001", "张三", 80, 88, 90); Student stu2 = new Student("002", "李四", 85, 90, 87); studentList.add(stu1); studentList.add(stu2); // 设置表格样式 TableStyle style = new TableStyle(); style.setTableHeadFont(getFontHeight((short) 12, "黑体")); style.setTableContentBackGroundColor(IndexedColors.WHITE); style.setTableHeadBackGroundColor(IndexedColors.PINK1); style.setTableTitleBackGroundColor(IndexedColors.GOLD); style.setTableTitleFont(getFontHeight((short) 16, "楷体_GB2312")); style.setTableHeadSecondBackGroundColor(IndexedColors.LIGHT_YELLOW); style.setTableHeadThreeBackGroundColor(IndexedColors.LIGHT_ORANGE); // 表头分两行,第一行3列,第二行2列 style.setHeadRows(1); style.setSecondHeadRows(1); // 导出Excel文件 ExportParams params = new ExportParams("学生信息表", "学生信息", ExcelType.XSSF); params.setStyle(style); Workbook workbook = ExcelExportUtil.exportExcel(params, Student.class, studentList); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(params.getExcelName(), "UTF-8")); response.flushBuffer(); workbook.write(response.getOutputStream()); } ③ 运行程序即可看到导出的Excel文件中含有多级表头。 2. 动态表头导出 动态表头就是表头中包含多个级别,其中每个级别的列数是不固定的,会根据实际数据动态增加。在Easypoi中,我们可以通过定义NestedColumn注解的方式动态设置表头名称和列数,具体实现步骤如下: ① 在需要导出的实体类中定义表头,例如: public class Student { @Excel(name = "学号", orderNum = "0", width = 15) private String id; @Excel(name = "姓名", orderNum = "1", width = 15) private String name; @Excel(name = "成绩", orderNum = "2", width = 15) @NestedColumn(nestedColumns = {@Excel(name = "语文", orderNum = "1", width = 15), @Excel(name = "数学", orderNum = "2", width = 15), @Excel(name = "英语", orderNum = "3", width = 15)}) private List<Double> scores; } ② 在需要导出的Controller中设置表格样式,例如: @GetMapping("/exportStudent") public void exportStudent(HttpServletResponse response) throws IOException { // 模拟数据 List<Student> studentList = new ArrayList<>(); Student stu1 = new Student("001", "张三", Arrays.asList(80.0, 88.0, 90.0)); Student stu2 = new Student("002", "李四", Arrays.asList(85.0, 90.0, 87.0)); studentList.add(stu1); studentList.add(stu2); // 设置表格样式 TableStyle style = new TableStyle(); style.setTableHeadFont(getFontHeight((short) 12, "黑体")); style.setTableContentBackGroundColor(IndexedColors.WHITE); style.setTableHeadBackGroundColor(IndexedColors.PINK1); style.setTableTitleBackGroundColor(IndexedColors.GOLD); style.setTableTitleFont(getFontHeight((short) 16, "楷体_GB2312")); style.setTableHeadSecondBackGroundColor(IndexedColors.LIGHT_YELLOW); style.setTableHeadThreeBackGroundColor(IndexedColors.LIGHT_ORANGE); // 导出Excel文件 ExportParams params = new ExportParams("学生信息表", "学生信息", ExcelType.XSSF); params.setStyle(style); Workbook workbook = ExcelExportUtil.exportExcel(params, Student.class, studentList); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(params.getExcelName(), "UTF-8")); response.flushBuffer(); workbook.write(response.getOutputStream()); } ③ 运行程序即可看到动态表头导出结果。 以上就是Easypoi导出Excel多级表头的实现方法。对于不同类型的表头,我们可以根据需要选择对应的方式实现,使得表格样式更加美观、易于理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值