特点
- 一看就懂的excel操作工具
demo
gitee地址
https://gitee.com/ichiva/easy-excel-demo.git
主要依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.5</version>
</dependency>
编写测试用例
实体类
@Data
public class Student {
//设置excel表头名称和顺序
@ExcelProperty(value="学生编号",index=1)
private Integer sno;
@ExcelProperty(value="学生姓名",index=0)
private String sname;
}
编写监听器
public class ExcelListener extends AnalysisEventListener<Student> {
//一行一行读取excel内容
@Override
public void invoke(Student data, AnalysisContext analysisContext) {
System.out.println("*****" + data);
}
//读取表头内容
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头: " + headMap);
}
//读取完成之后
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.println("数据读完了");
}
}
写入数据
String filename = "./write.xlsx";
@Test
public void write() {
List<Student> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Student data = new Student();
data.setSno(i);
data.setSname("xxx"+i);
list.add(data);
}
EasyExcel.write(filename, Student.class).sheet("学生列表").doWrite(list);
}
读取数据
String filename = "./write.xlsx";
@Test
public void read() {
EasyExcel.read(filename, Student.class,new ExcelListener()).sheet().doRead();
}
总结,excel工具自己也封装过,当发现这款工具时就觉得自己以前封装的很别扭 PS.阿里的亲儿子,可以方向使用
gitee地址
https://gitee.com/ichiva/easy-excel-demo.git
来源:oschina
链接:https://my.oschina.net/u/4661459/blog/4667298