EsayExcel读
引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.1</version> </dependency>
读
private List<Map<String, String>> excelToListMap(MultipartFile file) { //返回值 List<Map<String,String>> excelList = new ArrayList<>(); //表头 Map<Integer,String> headMapShow = new HashMap<>(); int count=0; try { EasyExcel.read(file.getInputStream()) .sheet() .registerReadListener(new AnalysisEventListener<Map<Integer,String>>() { @Override public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) { //这个是每读取一行执行一次的,在invokeHeader之后执行,读取的行数据会生成一个Map,key值是列,value值数据 Map<String,String> 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<Integer, String> 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; }