模板
使用指定的模板导出数据:
这个是复杂的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} | |||||||
代码
package com.hbte.sharp;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import org.junit.jupiter.api.Test;
import java.util.*;
public class EasyExcelTest {
/**
* 测试填充复杂excel表单的例子
*/
@Test
public void testMultiExcel() {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
// {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
String templatePath = "C:\\Users\\DELL\\Desktop\\测试 easyExcel 填充.xlsx";
List<Demo> person = Arrays.asList(new Demo(), new Demo(), new Demo(), new Demo(),
new Demo(), new Demo(), new Demo(), new Demo(),
new Demo(), new Demo(), new Demo(), new Demo());
String love = "xiaohui hui 小灰灰 小辉 " + new Random().nextLong();
String copyright = "版权所有,子虚乌有 " + new Random().nextLong();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
// 如果有多个list 模板上必须有{前缀.} 这里的前缀就是 data1,然后多个list必须用 FillWrapper包裹
ExcelWriter excelWriter = EasyExcel.write("小辉的测试" + new Random().nextLong() + ".xlsx")
.withTemplate(templatePath)
// .needHead(false)
// .autoCloseStream(true)
// .useDefaultStyle(false)
.excelType(ExcelTypeEnum.XLSX).build();
excelWriter.fill(new FillWrapper("person", person), fillConfig, writeSheet);
// 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
// forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
// 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
// 如果数据量大 list不是最后一行 参照下一个
excelWriter.fill(new FillWrapper("student", person), FillConfig.builder().forceNewRow(true).build(), writeSheet);
excelWriter.fill(new FillWrapper("temp", person), FillConfig.builder().forceNewRow(true).build(), writeSheet);
Map<String, Object> map = new HashMap<String, Object>();
map.put("love", love);
map.put("copyright", copyright);
map.put("title", copyright);
// 填充单个值
excelWriter.fill(map, writeSheet);
excelWriter.finish();
}
}
class Demo {
private Long seq = 1L + new Random().nextLong();
private Long id = 2L + new Random().nextLong();
private String name = "xiaohui" + new Random().nextLong();
private Date birthday = new Date();
private Double score = 876.14D;
private Short age = (short) (10 + new Random().nextInt(100));
private String remark = "zheli是注释说明" + UUID.randomUUID().toString();
private String no = UUID.randomUUID().toString();
private String className = UUID.randomUUID().toString().substring(0, 8);
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Long getSeq() {
return seq;
}
public void setSeq(Long seq) {
this.seq = seq;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
小结
easyExcel 工具分三种使用场景:
- read,导入读取数据
- write, 导出,写入数据
- fill,填充,根据模板填充数据
easyExcel 还挺好用,多看它各种 handler,自定义处理数据(包括样式,数据的汇聚处理,表头等)