引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
编写模板
编写代码
public class FillData {
private String name;
private Double number;
public FillData(String name, Double number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getNumber() {
return number;
}
public void setNumber(Double number) {
this.number = number;
}
}
注意:
列表填充一定要用实体对象,不能用map
@RequestMapping("/test")
public void test() {
// 模板注意 用{} 来表示你要用的变量,{.}表示列表 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
String templateFileName = "D:\\tools\\code\\excel\\demofillsimple.xlsx";
String fileName = "D:\\tools\\code\\excel\\simpleFill" + System.currentTimeMillis() + ".xlsx";
List<FillData> list = new ArrayList<>();
FillData s1 = new FillData("张三", 20.0);
FillData s2 = new FillData("李四", 50.0);
list.add(s1);list.add(s2);
FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();
ExcelWriter writer = EasyExcel.write(fileName).withTemplate(templateFileName).build();
WriteSheet sheet1 = EasyExcel.writerSheet().sheetName("1").build();
// 这里 会填充到第一个sheet页为名称为 1,
writer.fill(list,fillConfig,sheet1);
WriteSheet sheet2 = EasyExcel.writerSheet().sheetName("2").build();
// 这里 会填充到第一个sheet页为名称为 2,
writer.fill(list,fillConfig, sheet2);
writer.finish();
}
验证