流程简述
1、创建一个普通的maven项目
2、导入easyExcel包
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.10</version></dependency>
3、打开需要导入的Excel表格,视线来到标题列
4、创建一个实体类并使用 EasyExcel 提供的注解 @ExcelProperty 与 Excel 表对应(注意:需要实现 get 、set 方法,不然对象可能获取不到数据)
public class ExcelDate {@ExcelProperty(value = "车牌")String plateNum;@ExcelProperty(value = "罚款金额")String amount;@ExcelProperty(value = "罚款缘由")String reason;public String getPlateNum() {return plateNum;}public void setPlateNum(String plateNum) {this.plateNum = plateNum;}public String getAmount() {return amount;}public void setAmount(String amount) {this.amount = amount;}public String getReason() {return reason;}public void setReason(String reason) {this.reason = reason;}@Overridepublic String toString() {return "ExcelDate{" + "plateNum='" + plateNum + '\'' + ", amount='" + amount + '\'' + ", reason='" + reason + '\'' + '}';}
}
5、创建一个启动类,运行,即可读取
public class EasyExcelImportMain {public static void main(String[] args) throws Exception {// 创建一个输入流,将 Excel 文件读取出来InputStream inputStream = new FileInputStream("C:\\Users\\admin\\Desktop\\1.xlsx");List<ExcelDate> tmpList = EasyExcel.read(inputStream)// 设置与Excel表映射的类.head(ExcelDate.class)// 设置sheet,默认读取第一个.sheet()// 设置标题所在行数.headRowNumber(1)// 异步读取.doReadSync();for (ExcelDate tmpDate : tmpList) {System.out.println(tmpDate);}}
}
6、运行结果