最近项目中遇到一个复杂的Excel的导入,并且数据量较大。因为数据不规则,所以只能使用POI进行自定义读取,但是发现数据量大之后,读取数据非常耗时。后面换成EasyExcel,性能起飞。
1. Excel样板
如上图,需要导入学校学生信息。前面三列,固定为学校班级信息,可以理解为主表。从第四列开始,为学生信息,其中一列为一条子表信息。
2. 具体实现
- 引入EasyExcel的依赖
<dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>3.3.2</version>
</dependency>
- 自定义导入监听器
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;public class ImportExcelListener extends AnalysisEventListener<Object> {// 封装数据 private List<Object> list = new ArrayList<>();// sheet页索引private int sheetNo = 0;@Overridepublic void invoke(Object t,AnalysisContext context) {// 读取Excel内容int currentSheetNo = context.readSheetHolder().getSheetNo();if(currentSheetNo != sheetNo){// 根据sheet页索引,防止重复添加数据list = new ArrayList<>();sheetNo = currentSheetNo;}list.add(t);}/*** 将表格转化为map集合(复杂Excel)*/public List<LinkedHashMap> getListMap(){String jsonObj = JSON.toJSONString(list);return JSON.parseArray(jsonObj,LinkedHashMap.class);}/*** 读取完数据后的操作*/@Overridepublic void doAfterAllAnalysed(AnalysisContext context){}
}
- 测试使用
3.1. TestController
@RestController
public class ExcelContorller{@Autowiredprivate ExcelService excelService;@PostMapping("/testImport")public Boolean importExcel(MultipartFile file) throws IOException {return excelService.importExcel(file);}
}
3.2. ExcelService
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.test.excel.bean.SchoolInfo;
import com.test.excel.listener.ImportExcelListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.StopWatch;import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;@Slf4j
@Service
public class ExcelService {/*** 导入*/public boolean importExcel(MultipartFile file) throws IOException {log.debug("进入导出方法");StopWatch watch = new StopWatch();watch.start();ImportExcelListener importListener = new ImportExcelListener ();ExcelReader excelReader = null;try {// 1为跳过标题行数excelReader = EasyExcelFactory.read(file.getInputStream,importListener).headRowNumber(1).build();List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();List<SchoolInfo> dataList = new ArrayList<>();// 循环处理Sheetfor (ReadSheet sheet : sheets){excelReader.read(sheet);this.dealSheetData(importListener.getListMap(),dataList);}}catch(Exception ex){log.error("导入失败",ex)}finally{watch.stop();log.debug("导入耗时{}毫秒",watch.getTotalTimeMilis());}}
}/*** 数据处理*/
private void dealSheetData(List<LinkedHashMap> rowList,List<SchoolInfo> resultList) {int rowSize = rowList.size();for(int i=0; i < rowSize; i++){// 这里为一行的数据,map的key为单元格的下标,从0开始,value为对应单元格的值LinkedHashMap<Integer,String> row = rowList.get(i);// 数据处理...}
}