不创建对象的读

 

步骤:

(1)首先获取文件所在路径

(2)自定义监听器(在监听器中对数据进行处理,由于默认一行行的读取excel,所以需要创建excel一行一行的回调监听器)

(3)读取数据

 

引入依赖:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
        </dependency>

 

代码示例:

public String testUploadFile(HttpServletRequest request) throws SysException{
        ApiResult result = new ApiResult();
        result.setCode(ApiResult.SUCCESS_CODE);
        result.setMessage(i18n(QUERY_SUCCESS_MSG));
        try {
            //获取文件所在路径
            File file = new File("D:\\test.xlsx");
            //一行一行解析,读取一行调用一次监听器
            //添加 headRowNumber(1),指定标题行的行数,否则默认为1不从第一行开始读
            EasyExcel.read(file,new NoModelDataListener()).sheet().headRowNumber(0).doRead();
        }catch (Exception e) {
            result.setCode(HttpStatus.EXPECTATION_FAILED.value());
            result.setMessage("上传失败");
            logger.error("", e);
            throw new RuntimeException();
        }
        return result.toJsonStr();
    }

 

定义监听器:

public class NoModelDataListener extends AnalysisEventListener<Map<Integer, String>> {
    private static Logger logger = LoggerFactory.getLogger(DataDicItemApi.class);
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    DataDicOptionMVO optionMVO = new DataDicOptionMVO();

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        //excel的行号
        Integer rowIndex=context.readRowHolder().getRowIndex();
        Set set = data.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println("key=="+next+"value=="+data.get(next));
        }
        cachedDataList.add(data);

        // 获取总行数(含表头)
        Integer rowNumber = context.readSheetHolder().getApproximateTotalRowNumber();
        System.out.println("excel总行数=="+rowNumber);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        logger.info("数据解析完成!");
    }
}

 

  使用easyExcel读取excel比较简单,比使用poi节省很多代码(需要逐行获取数据,再获取该行的单元格数据,然后对数据进行处理),而使用easyExcel只需要应用监听器,在监听器方法中逐行根据键(列号)获取对应值的数据然后处理即可。

文档:https://easyexcel.opensource.alibaba.com/docs/current/

 


 

自定义map缓存

  如果map包含所属目录直接用,如果没有,先存入map缓存然后再取出使用

List<DataDicItemExcel> itemList = dataDicItemMDAO.exportExcel(dataDicItem);
        Map<String,String> map = new HashMap<>();
        for (DataDicItemExcel dataDicItemMVO : itemList) {
            String catalogName = "";
            //判断是否包含目录id
            if(map.containsKey(dataDicItemMVO.getDataDicCatalogId())){
                catalogName = map.get(dataDicItemMVO.getDataDicCatalogId());
            }else{
                //不包含先存入再获取
                map.put(dataDicItemMVO.getDataDicCatalogId(),dataDicItemMDAO.queryCatalogs(dataDicItemMVO.getDataDicCatalogId()));
                catalogName = map.get(dataDicItemMVO.getDataDicCatalogId());
            }
            dataDicItemMVO.setTableCatalog(catalogName);
        }