上传EXCEL是所有字段的实体对象
typescript复制代码package com.example.demo;
public class SingleDialog {
private String standard;
private String similar;
private String answer;
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public String getSimilar() {
return similar;
}
public void setSimilar(String similar) {
this.similar = similar;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
定义一个ExcelHander继承AnalysisEventListener最重要的是重写invoke方法,去执行读EXCEL逻辑
java复制代码package com.example.demo;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
public class ExcelHander extends AnalysisEventListener<SingleDialog> {
private List SingleDialogs;
public ExcelHander(List singleDialogs) {
SingleDialogs = singleDialogs;
}
public void invoke(SingleDialog singleDialog, AnalysisContext analysisContext) {
SingleDialogs.add(singleDialog);
}
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
System.out.print("成功");
}
}
调用逻辑
java复制代码package com.example.demo;
import com.alibaba.excel.EasyExcel;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.*;
public class TranslateUtils {
public static void main(String[] args) {
translate("C:\\Desktop\\导出\\book.xls");
}
public static void translate(String path) {
List<SingleDialog>singleDialogs=new ArrayList<SingleDialog>();
ExcelHander handle=new ExcelHander(singleDialogs);
FileInputStream fs = new FileInputStream(new File(path));
EasyExcel.read(fs,SingleDialog.class,handle).sheet("Sheet1").doRead();
//读取成功
System.out.println(singleDialogs.size())
}
}
- 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.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.