Search in sources :

Example 1 with XlsReadWorkbookHolder

use of com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder in project easyexcel by alibaba.

the class BofRecordHandler method processRecord.

@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);
        xlsReadWorkbookHolder.setIgnoreRecord(Boolean.FALSE);
        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 actualReadSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex);
    assert actualReadSheet != null : "Can't find the sheet.";
    // Copy the parameter to the current sheet
    ReadSheet readSheet = SheetUtils.match(actualReadSheet, xlsReadContext);
    if (readSheet != null) {
        xlsReadContext.currentSheet(readSheet);
        xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.FALSE);
    } else {
        xlsReadContext.xlsReadWorkbookHolder().setIgnoreRecord(Boolean.TRUE);
    }
    // Go read the next one
    xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
}
Also used : XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) BOFRecord(org.apache.poi.hssf.record.BOFRecord) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 2 with XlsReadWorkbookHolder

use of com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder in project easyexcel by alibaba.

the class XlsSaxAnalyser method execute.

@Override
public void execute() {
    XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
    MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
    xlsReadWorkbookHolder.setFormatTrackingHSSFListener(new FormatTrackingHSSFListener(listener));
    EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
    xlsReadWorkbookHolder.setHssfWorkbook(workbookBuildingListener.getStubHSSFWorkbook());
    HSSFEventFactory factory = new HSSFEventFactory();
    HSSFRequest request = new HSSFRequest();
    request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
    try {
        factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem());
    } catch (IOException e) {
        throw new ExcelAnalysisException(e);
    }
}
Also used : HSSFEventFactory(org.apache.poi.hssf.eventusermodel.HSSFEventFactory) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) MissingRecordAwareHSSFListener(org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener) HSSFRequest(org.apache.poi.hssf.eventusermodel.HSSFRequest) IOException(java.io.IOException) EventWorkbookBuilder(org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder) FormatTrackingHSSFListener(org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Example 3 with XlsReadWorkbookHolder

use of com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder in project easyexcel by alibaba.

the class ExcelAnalyserImpl method finish.

@Override
public void finish() {
    if (finished) {
        return;
    }
    finished = true;
    if (analysisContext == null || analysisContext.readWorkbookHolder() == null) {
        return;
    }
    ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
    Throwable throwable = null;
    try {
        if (readWorkbookHolder.getReadCache() != null) {
            readWorkbookHolder.getReadCache().destroy();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder) && ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage() != null) {
            ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage().revert();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if ((readWorkbookHolder instanceof XlsReadWorkbookHolder) && ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem() != null) {
            ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    // https://github.com/alibaba/easyexcel/issues/2309
    try {
        if ((readWorkbookHolder instanceof CsvReadWorkbookHolder) && ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser() != null) {
            ((CsvReadWorkbookHolder) readWorkbookHolder).getCsvParser().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if (analysisContext.readWorkbookHolder().getAutoCloseStream() && readWorkbookHolder.getInputStream() != null) {
            readWorkbookHolder.getInputStream().close();
        }
    } catch (Throwable t) {
        throwable = t;
    }
    try {
        if (readWorkbookHolder.getTempFile() != null) {
            FileUtils.delete(readWorkbookHolder.getTempFile());
        }
    } catch (Throwable t) {
        throwable = t;
    }
    clearEncrypt03();
    removeThreadLocalCache();
    if (throwable != null) {
        throw new ExcelAnalysisException("Can not close IO.", throwable);
    }
}
Also used : CsvReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder) ReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder) XlsxReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder) CsvReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) XlsReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder) XlsxReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder) ExcelAnalysisException(com.alibaba.excel.exception.ExcelAnalysisException)

Aggregations

XlsReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder)3 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)2 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 ReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder)1 CsvReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder)1 XlsxReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder)1 IOException (java.io.IOException)1 EventWorkbookBuilder (org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder)1 FormatTrackingHSSFListener (org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener)1 HSSFEventFactory (org.apache.poi.hssf.eventusermodel.HSSFEventFactory)1 HSSFRequest (org.apache.poi.hssf.eventusermodel.HSSFRequest)1 MissingRecordAwareHSSFListener (org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1