当前位置:   article > 正文

@ExcelProperty 使用占位符,实现动态表头效果

@excelproperty

@ExcelProperty 使用占位符,实现动态表头效果

@ExcelProperty 使用占位符,实现动态表头效果

easyexcel 想实现动态表头,根据官方文档,采用以下方式:

 private List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("字符串" + System.currentTimeMillis());
        List<String> head1 = new ArrayList<String>();
        head1.add("数字" + System.currentTimeMillis());
        List<String> head2 = new ArrayList<String>();
        head2.add("日期" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

个人感觉该方式不够灵活,且实体类中定义的样式也失效了,如果表格字段较多,需要写一大堆List。经过研究,通过另一种方式实现动态表头效果。

定义导出实体类,此处省略get、set方法

public class UserModel {

    @ExcelProperty(value = {"${title}", "姓名"})
    private String name;

    @ExcelProperty(value = {"${title}", "生日"})
    @DateTimeFormat(value = "yyyy-MM-dd")
    private Date birth;

    @ExcelProperty(value = {"${title}", "年龄"})
    private int age;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

${title}表示需要设置的标题

自定义CustomerTitleHandler

自定义CustomerTitleHandler,实现CellWriteHandler接口,重写beforeCellCreate方法,使用PropertyPlaceholderHelper 类将实体类中的占位符替换为真实的title

public class CustomerTitleHandler implements CellWriteHandler {
    private String title;
    PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("${", "}");
    public CustomerTitleHandler(String title) {
        this.title = title;
    }
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {
        if (head=null) {
            List<String> headNameList = head.getHeadNameList();
            if (CollectionUtils.isNotEmpty(headNameList)) {
                Properties properties = new Properties();
                properties.setProperty("title", title);
                headNameList.set(0, placeholderHelper.replacePlaceholders(headNameList.get(0), properties));
            }
        }
    }
    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }
    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                       CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

创建ExcelWriter,生成excel文件

public class EasyExcelTest {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\ambit\\Desktop\\user.xlsx");
         String title = "用户信息112233";
        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(file, UserModel.class).excelType(ExcelTypeEnum.XLSX).build();
            WriteSheet sheet = EasyExcel.writerSheet().registerWriteHandler(new CustomerTitleHandler(title)).build();
            List<UserModel> list;
            for (int i = 0; i < 5; i++) {
                list = new ArrayList<>(1);
                UserModel userModel = new UserModel();
                userModel.setName("张三" + i);
                userModel.setBirth(new Date());
                userModel.setAge(18 + i);
                list.add(userModel);
                excelWriter.write(list, sheet);
            }
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

最终效果如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码独立开发者/article/detail/61127
推荐阅读
相关标签
  

闽ICP备14008679号