标签:dem xlsx contex 调用 dep 信息 group ext after
<!--引入EasyExcel,同时需要引入poi的包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class DemoData {
//设置excel表头的名称
//0代表第一列
@ExcelProperty(value = "学生学号",index = 0)
private Integer sno;
//1代表第二列
@ExcelProperty(value = "学生姓名",index = 1)
private String sname;
}
public class TestEasyExcel {
public static void main(String[] args) {
//实现excel写操作
//1.设置写入文件夹的地址和excel文件名称
//String fileName="D:\\excel\\write.xlsx";
//2.调用eastExcel里面方法实现
//参数一:文件路径名称
//参数二:实体类的class
//EasyExcel.write(fileName,DemoData.class).sheet("学生信息").doWrite(getData());
//读操作
//1.设置读的文件夹的地址和excel文件名称
String fileName="D:\\excel\\write.xlsx";
EasyExcel.read(fileName, DemoData.class, new ExcelListener()).sheet().doRead();
}
private static List<DemoData> getData(){
List<DemoData> list=new ArrayList<>();
for(int i=0;i<10;i++){
DemoData demoData=new DemoData();
demoData.setSno(i);
demoData.setSname("Tom"+i);
list.add(demoData);
}
return list;
}
}
建立实体类,同上
设置监听器
public class ExcelListener extends AnalysisEventListener<DemoData> {
//表示一行一行的读取excel的内容,不读表头
@Override
public void invoke(DemoData demoData, AnalysisContext analysisContext) {
System.out.println("****"+demoData);
}
//读取表头里面的东西
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
System.out.println("表头"+headMap);
}
//读取完成之后完成事情
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
//1.设置读的文件夹的地址和excel文件名称
String fileName="D:\\excel\\write.xlsx";
EasyExcel.read(fileName, DemoData.class, new ExcelListener()).sheet().doRead();
标签:dem xlsx contex 调用 dep 信息 group ext after
原文地址:https://www.cnblogs.com/HezhenbinGoGo/p/14305213.html