模板

使用指定的模板导出数据:

这个是复杂的excel填充 {title}
姓名 {person.name}
年龄 {person.age}
生日 {person.birthday}
学号 名字 班级 成绩
{student.no} {student.name} {student.className} {student.score}
表格制作人:你的最爱 {love}
临时的表格
序号 id 名称 备注
{temp.seq} {temp.id} {temp.name} {temp.remark}
版权信息:{copyright}

image.png

代码

  1. package com.hbte.sharp;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.excel.ExcelWriter;
  4. import com.alibaba.excel.enums.WriteDirectionEnum;
  5. import com.alibaba.excel.support.ExcelTypeEnum;
  6. import com.alibaba.excel.write.metadata.WriteSheet;
  7. import com.alibaba.excel.write.metadata.fill.FillConfig;
  8. import com.alibaba.excel.write.metadata.fill.FillWrapper;
  9. import org.junit.jupiter.api.Test;
  10. import java.util.*;
  11. public class EasyExcelTest {
  12. /**
  13. * 测试填充复杂excel表单的例子
  14. */
  15. @Test
  16. public void testMultiExcel() {
  17. // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
  18. // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
  19. String templatePath = "C:\\Users\\DELL\\Desktop\\测试 easyExcel 填充.xlsx";
  20. List<Demo> person = Arrays.asList(new Demo(), new Demo(), new Demo(), new Demo(),
  21. new Demo(), new Demo(), new Demo(), new Demo(),
  22. new Demo(), new Demo(), new Demo(), new Demo());
  23. String love = "xiaohui hui 小灰灰 小辉 " + new Random().nextLong();
  24. String copyright = "版权所有,子虚乌有 " + new Random().nextLong();
  25. WriteSheet writeSheet = EasyExcel.writerSheet().build();
  26. FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
  27. // 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
  28. ExcelWriter excelWriter = EasyExcel.write("小辉的测试" + new Random().nextLong() + ".xlsx")
  29. .withTemplate(templatePath)
  30. // .needHead(false)
  31. // .autoCloseStream(true)
  32. // .useDefaultStyle(false)
  33. .excelType(ExcelTypeEnum.XLSX).build();
  34. excelWriter.fill(new FillWrapper("person", person), fillConfig, writeSheet);
  35. // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
  36. // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
  37. // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
  38. // 如果数据量大 list不是最后一行 参照下一个
  39. excelWriter.fill(new FillWrapper("student", person), FillConfig.builder().forceNewRow(true).build(), writeSheet);
  40. excelWriter.fill(new FillWrapper("temp", person), FillConfig.builder().forceNewRow(true).build(), writeSheet);
  41. Map<String, Object> map = new HashMap<String, Object>();
  42. map.put("love", love);
  43. map.put("copyright", copyright);
  44. map.put("title", copyright);
  45. // 填充单个值
  46. excelWriter.fill(map, writeSheet);
  47. excelWriter.finish();
  48. }
  49. }
  50. class Demo {
  51. private Long seq = 1L + new Random().nextLong();
  52. private Long id = 2L + new Random().nextLong();
  53. private String name = "xiaohui" + new Random().nextLong();
  54. private Date birthday = new Date();
  55. private Double score = 876.14D;
  56. private Short age = (short) (10 + new Random().nextInt(100));
  57. private String remark = "zheli是注释说明" + UUID.randomUUID().toString();
  58. private String no = UUID.randomUUID().toString();
  59. private String className = UUID.randomUUID().toString().substring(0, 8);
  60. public String getClassName() {
  61. return className;
  62. }
  63. public void setClassName(String className) {
  64. this.className = className;
  65. }
  66. public Short getAge() {
  67. return age;
  68. }
  69. public void setAge(Short age) {
  70. this.age = age;
  71. }
  72. public Long getSeq() {
  73. return seq;
  74. }
  75. public void setSeq(Long seq) {
  76. this.seq = seq;
  77. }
  78. public Long getId() {
  79. return id;
  80. }
  81. public void setId(Long id) {
  82. this.id = id;
  83. }
  84. public String getName() {
  85. return name;
  86. }
  87. public void setName(String name) {
  88. this.name = name;
  89. }
  90. public Date getBirthday() {
  91. return birthday;
  92. }
  93. public void setBirthday(Date birthday) {
  94. this.birthday = birthday;
  95. }
  96. public Double getScore() {
  97. return score;
  98. }
  99. public void setScore(Double score) {
  100. this.score = score;
  101. }
  102. public String getRemark() {
  103. return remark;
  104. }
  105. public void setRemark(String remark) {
  106. this.remark = remark;
  107. }
  108. public String getNo() {
  109. return no;
  110. }
  111. public void setNo(String no) {
  112. this.no = no;
  113. }
  114. }

小结

easyExcel 工具分三种使用场景:

  1. read,导入读取数据
  2. write, 导出,写入数据
  3. fill,填充,根据模板填充数据

easyExcel 还挺好用,多看它各种 handler,自定义处理数据(包括样式,数据的汇聚处理,表头等)