Java通过EasyExcel导出excel
第一步,引入pom依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
第二步,添加实体对象
package com.hzrc.clocksystem;
import com.alibaba.excel.annotation.ExcelProperty;
import java.io.Serializable;
public class TestEntity implements Serializable {
private static final long serialVersionUID = 3826270251349545589L;
@ExcelProperty(value = "测试id",index = 0)
private String id;
@ExcelProperty(value = "测试时间",index = 1)
private String createdTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCreatedTime() {
return createdTime;
}
public void setCreatedTime(String createdTime) {
this.createdTime = createdTime;
}
}
第三步,生成excel
package com.hzrc.clocksystem;
import com.alibaba.excel.EasyExcel;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestMain {
public static void main(String[] args) throws IOException {
String filename = "D:\\test\\test.xlsx";
EasyExcel.write(filename, TestEntity.class).sheet("测试列表").doWrite(getExcelData());
}
private static List<TestEntity> getExcelData() {
List<TestEntity> list = new ArrayList<>();
for (int i = 1; i <= 50; i++) {
TestEntity entity = new TestEntity();
entity.setId(String.valueOf(i));
entity.setCreatedTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:dd").format(new Date()));
list.add(entity);
}
return list;
}
}
第四步,效果呈现
知是行之始,行是知之成
|