MAVEN
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.0.5</version></dependency>
代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;// 定义你的数据类,用于映射Excel中的字段
public class YourDataClass {// 定义字段,与Excel列名对应@ExcelProperty(index = 1)private String field1;@ExcelProperty(index = 2)private String field2;// getters and setterspublic String getField1() {return field1;}public void setField1(String field1) {this.field1 = field1;}public String getField2() {return field2;}public void setField2(String field2) {this.field2 = field2;}@Overridepublic String toString() {return "YourDataClass{" +"field1='" + field1 + '\'' +", field2='" + field2 + '\'' +'}';}
}// 自定义监听器,用于处理读取到的数据
class YourDataListener extends AnalysisEventListener<YourDataClass> {@Overridepublic void invoke(YourDataClass data, AnalysisContext context) {System.out.println(data);// 处理每条数据,例如保存到数据库或其他操作}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {// 所有数据处理完毕后的操作System.out.println("doAfterAllAnalysed");}
}// 读取Excel文件
class YourExcelReader {public void readExcel(String fileName) {// 读取第一个sheetEasyExcel.read(fileName, YourDataClass.class, new YourDataListener()).sheet().doRead();}
}// 使用
class Main {public static void main(String[] args) {YourExcelReader reader = new YourExcelReader();reader.readExcel("C:\\Users\\28698\\Documents\\xxxxx.xlsx");}
}
https://www.baidu.com/s?wd=easyexcel%E5%A4%8D%E6%9D%82%E6%95%B0%E6%8D%AE&rsv_spt=1&rsv_iqid=0xb82331eb0011cb00&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=57095150_7_oem_dg&rsv_enter=1&rsv_dl=tb&oq=easy&rsv_btype=t&inputT=4213&rsv_t=2e30yPMD2lAzHObglPAtRziTwFCyCpVyR2ekIw73AnkB02dVwyDmvPN1zFYStYSU1xWJM84rWg0&rsv_sug3=22&rsv_sug1=17&rsv_sug7=100&rsv_pq=b65dc51501a09efe&rsv_sug2=0&rsv_sug4=4513