引入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.6</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency>
读取本地excell
//读取excellString fileUrl = "/Users/waee/Downloads/商品信息.xlsx";FileInputStream fileInputStream = new FileInputStream(fileUrl);List<GoodsExcellVO> goodsExcellVOList = parseExcel(fileInputStream);
private List<GoodsExcellVO> parseExcel(InputStream inputStream) {ExcelListener excelListener = new ExcelListener();EasyExcel.read(inputStream, GoodsExcellVO.class, excelListener).sheet().doRead();return excelListener.getExcelVOList();}
@Data
public class ExcelListener<T> extends AnalysisEventListener<T> {private List<T> excelVOList = new ArrayList<>();@Overridepublic void invoke(T t, AnalysisContext context) {excelVOList.add(t);}@Overridepublic void doAfterAllAnalysed(AnalysisContext context) {}
}
写一个接收的VO
@ExcelProperty(value = "商品名称", index = 0)