引入依赖
<dependency> <groupId>com.alibabagroupId> <artifactId>easyexcelartifactId> <version>3.0.1version> dependency>
private List> excelToListMap(MultipartFile file) { //返回值 List> excelList = new ArrayList<>(); //表头 Map headMapShow = new HashMap<>(); int count=0; try { EasyExcel.read(file.getInputStream()) .sheet() .registerReadListener(new AnalysisEventListener>() { @Override public void invoke(Map integerStringMap, AnalysisContext analysisContext) { //这个是每读取一行执行一次的,在invokeHeader之后执行,读取的行数据会生成一个Map,key值是列,value值数据 Map plan = new LinkedHashMap<>(); for (int i=0; i< headMapShow.size(); i++) { plan.put(headMapShow.get(i),integerStringMap.get(i)); } excelList.add(plan); } @Override public void invokeHeadMap(Map headMap, AnalysisContext context) { //这里是先执行的,会返回一个表头的Map,key值是列,value值是表头 super.invokeHeadMap(headMap, context); headMapShow.putAll(headMap); } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { //这里是读取完全部列的时候执行的 } }) .doRead(); } catch (IOException e) { e.printStackTrace(); } return excelList; }