Search in sources :

Example 1 with ReadWorkbookHolder

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

the class SheetUtils method match.

/**
 * Match the parameters to the actual sheet
 *
 * @param readSheet
 *            actual sheet
 * @param analysisContext
 * @return
 */
public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) {
    ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
    if (readWorkbookHolder.getReadAll()) {
        return readSheet;
    }
    for (ReadSheet parameterReadSheet : readWorkbookHolder.getParameterSheetDataList()) {
        if (parameterReadSheet == null) {
            continue;
        }
        if (parameterReadSheet.getSheetNo() == null && parameterReadSheet.getSheetName() == null) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("The first is read by default.");
            }
            parameterReadSheet.setSheetNo(0);
        }
        boolean match = (parameterReadSheet.getSheetNo() != null && parameterReadSheet.getSheetNo().equals(readSheet.getSheetNo()));
        if (!match) {
            String parameterSheetName = parameterReadSheet.getSheetName();
            if (!StringUtils.isEmpty(parameterSheetName)) {
                boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) || (parameterReadSheet.getAutoTrim() == null && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
                if (autoTrim) {
                    parameterSheetName = parameterSheetName.trim();
                }
                match = parameterSheetName.equals(readSheet.getSheetName());
            }
        }
        if (match) {
            readSheet.copyBasicParameter(parameterReadSheet);
            return readSheet;
        }
    }
    return null;
}
Also used : ReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder) ReadSheet(com.alibaba.excel.read.metadata.ReadSheet)

Example 2 with ReadWorkbookHolder

use of com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder 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

ReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder)2 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 ReadSheet (com.alibaba.excel.read.metadata.ReadSheet)1 CsvReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder)1 XlsReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder)1 XlsxReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder)1