建立springboot项目
依赖:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.0-RC1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
public static void main(String[] args) {
//实现excel写的操作
//1 设置写入文件夹地址和excel文件名称
String filename = "E:\\zhuimain\\testEasyEXcal\\write.xlsx";
// 2 调用easyexcel里面的方法实现写操作
// write方法两个参数:第一个参数文件路径名称,第二个参数实体类class getData():自己的操作的数据
EasyExcel.write(filename, TestSaveEntity.class).sheet("学生列表").doWrite(getData());
}
//创建方法返回list集合 测试数据
private static List<TestSaveEntity> getData() {
List<TestSaveEntity> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String m=String.valueOf(i);
String age=String.valueOf(i+10);
TestSaveEntity data = new TestSaveEntity();
data.setId(m);
data.setAge(age);
data.setName("lucy"+m);
list.add(data);
}
return list;
}
最后效果: