alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.51k stars 7.54k forks source link

读sheet就会调用doAfterAllAnalysed吗 #589

Closed waitting49 closed 5 years ago

waitting49 commented 5 years ago

代码

private void saveData() {
    log.info("{}条数据,开始存储数据库!", list.size());
    for (int i = 0, size = list.size(); i < size; i++) {
        log.info(i + ":" + JSONObject.toJSONString(list.get(i)));
    }
    log.info("存储数据库成功!");
}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {
    saveData();
    log.info("所有数据解析完成!");
}

   public static <T> void read(InputStream input, Class<?> modelClass, AnalysisEventListener<T> listener) {
    ExcelReader excelReader = EasyExcel.read(input, modelClass, listener).build();
    excelReader.read();
    /*
     * int size = excelReader.excelExecutor().sheetList().size(); for (int i = 0; i
     * < size; i++) { ReadSheet readSheet =
     * excelReader.excelExecutor().sheetList().get(i); excelReader.read(readSheet);
     * }
     */
    excelReader.finish();
}

**日志提示**
 1条数据,开始存储数据库!
 0:{"userName":"gcme","userPhone":"18368873354","userRealName":"顾","userSexValue":"男","userUnitName":"test"}
 所有数据解析完成!
2条数据,开始存储数据库!
 0:{"userName":"gcme","userPhone":"18368873354","userRealName":"顾","userSexValue":"男","userUnitName":"test"}
1:{"userName":"gcme","userPhone":"18365574567","userRealName":"写","userSexValue":"女","userUnitName":"test"}
存储数据库成功!
所有数据解析完成!
3条数据,开始存储数据库!
 0:{"userName":"gcme","userPhone":"18368873354","userRealName":"顾","userSexValue":"男","userUnitName":"test"}
 1:{"userName":"gcme","userPhone":"18365574567","userRealName":"写","userSexValue":"女","userUnitName":"test"}
2:{"userName":"gcme","userPhone":"18365574567","userRealName":"做","userSexValue":"女","userUnitName":"test"}
 存储数据库成功!
所有数据解析完成!
**问题描述**
excel有三个sheet,每个sheet一条数据。读取sheet时就调用了doAfterAllAnalysed了吗?saveData调用了3次。请问有直接所有sheet读取完了再操作业务的方法吗?
zhuangjiaju commented 5 years ago

doAfterAllAnalysed在整个sheet 读取完毕后会调用。

Fuxy-pg commented 4 years ago

请问,如果想要在读取完所有sheet之后,在调用doAfterAllAnalysed方法,该怎么配置呢?