pom.xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
main.java
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import java.util.ArrayList;
import java.util.List;
public class InsertDemo {
public static void main(String[] args) {
String fileName = "dd.xlsx";
try (ExcelWriter excelWriter = EasyExcel.write(fileName, DataInfo.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
List<DataInfo> data = list_data();
excelWriter.write(data, writeSheet);
}
}
private static List<DataInfo> list_data()
{
List<DataInfo> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
DataInfo dataInfo = new DataInfo();
dataInfo.setId(i);
dataInfo.setName("张三");
dataInfo.setAddress("中国北京");
list.add(dataInfo);
}
return list;
}
}
DataInfo.java
public class DataInfo {
private Integer id;
private String name;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public DataInfo() {
}
}
参考链接
WriteTest