alibaba / easyexcel

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

读多个sheet 方式二 我理解的是有多个sheet时,可指定sheet读取,但是读取的时候可能会报错 #1457

Closed gaizishu closed 4 years ago

gaizishu commented 4 years ago

建议先去看文档 快速开始常见问题 触发场景描述 读多个sheet,采用方式二,理解可指定sheet读取,在使用时发现,如果第一个sheet没有读取的话,直接读取后面的sheet,会报错,报的是空指针 触发Bug的代码

#BofRecordHandler中  xlsReadContext.xlsReadSheetHolder().setIgnoreRecord(Boolean.TRUE); 报空指针,xlsReadSheetHolder为null
       @Override
    public void processRecord(XlsReadContext xlsReadContext, Record record) {
        BOFRecord br = (BOFRecord)record;
        XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
        if (br.getType() == BOFRecord.TYPE_WORKBOOK) {
            xlsReadWorkbookHolder.setReadSheetIndex(null);
            return;
        }
        if (br.getType() != BOFRecord.TYPE_WORKSHEET) {
            return;
        }
        // Init read sheet Data
        initReadSheetDataList(xlsReadWorkbookHolder);
        Integer readSheetIndex = xlsReadWorkbookHolder.getReadSheetIndex();
        if (readSheetIndex == null) {
            readSheetIndex = 0;
            xlsReadWorkbookHolder.setReadSheetIndex(readSheetIndex);
        }
        ReadSheet readSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex);
        assert readSheet != null : "Can't find the sheet.";
        // Copy the parameter to the current sheet
        readSheet = SheetUtils.match(readSheet, xlsReadContext);
        if (readSheet != null) {
            xlsReadContext.currentSheet(readSheet);
            xlsReadContext.xlsReadSheetHolder().setIgnoreRecord(Boolean.FALSE);
        } else {
            xlsReadContext.xlsReadSheetHolder().setIgnoreRecord(Boolean.TRUE);
        }
        // Go read the next one
        xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
    }

提示的异常或者没有达到的效果 java.lang.NullPointerException: null at com.alibaba.excel.analysis.v03.handlers.BofRecordHandler.processRecord(BofRecordHandler.java:49) ~[easyexcel-2.2.2.jar:?] at com.alibaba.excel.analysis.v03.XlsSaxAnalyser.processRecord(XlsSaxAnalyser.java:158) ~[easyexcel-2.2.2.jar:?] at org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener.processRecord(MissingRecordAwareHSSFListener.java:203) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener.processRecord(FormatTrackingHSSFListener.java:92) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.HSSFRequest.processRecord(HSSFRequest.java:109) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.genericProcessEvents(HSSFEventFactory.java:179) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processEvents(HSSFEventFactory.java:136) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processWorkbookEvents(HSSFEventFactory.java:82) ~[poi-3.17.jar:3.17] at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processWorkbookEvents(HSSFEventFactory.java:54) ~[poi-3.17.jar:3.17] at com.alibaba.excel.analysis.v03.XlsSaxAnalyser.execute(XlsSaxAnalyser.java:137) ~[easyexcel-2.2.2.jar:?] at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:115) ~[easyexcel-2.2.2.jar:?] at com.alibaba.excel.ExcelReader.read(ExcelReader.java:182) ~[easyexcel-2.2.2.jar:?] at cn.ucmed.advice.util.EasyExcelUtils.readAllSheets(EasyExcelUtils.java:157) ~[classes/:?]

zhuangjiaju commented 4 years ago

是否是最新版本?我记得这bug 已经改了。