赞
踩
@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;
}
个人感觉该方式不够灵活,且实体类中定义的样式也失效了,如果表格字段较多,需要写一大堆List。经过研究,通过另一种方式实现动态表头效果。
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;
}
${title}表示需要设置的标题
自定义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) { } }
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(); } } } }
最终效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。