导入示例
参考:https://alibaba-easyexcel.github.io/quickstart/read.html
# 读操作
使用写操作的结果文件,进行读取操作
修改bean,设置读取的索引值
package com.stt.demo;
import com.alibaba.excel.annotation.ExcelProperty;
public class Student {
public Student() {
}
public Student(Integer no, String name) {
this.no = no;
this.name = name;
}
// 设置excel列名称
// value 用于写操作时的列名称,而index可以指定读取的列编号
@ExcelProperty(value = "学生编号", index = 0)
private Integer no;
@ExcelProperty(value = "学生姓名", index = 1)
private String name;
public Integer getNo() {
return no;
}
public void setNo(Integer no) {
this.no = no;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 监听器
实现特定类的监听器,实现不同的读取转换方式
package com.stt.demo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.Map;
public class ExcelEventListener extends AnalysisEventListener<Student> {
// 读取表头
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
// 读取表头信息
System.out.println("表头信息: " + headMap);
}
// 一行一行的读取excel内容,从第二行开始读取,第一行是列名称
@Override
public void invoke(Student student, AnalysisContext analysisContext) {
System.out.println(student);
}
// 读取之后进行处理
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 读取逻辑
package com.stt.demo;
import com.alibaba.excel.EasyExcel;
public class StudentExcelRead {
public static void main(String[] args) {
String fileName = "d:/student.xlsx";
EasyExcel.read(fileName, Student.class, new ExcelEventListener())
.sheet()
.doRead();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Last Updated: 2022/01/16, 11:29:51