Search in sources :

Example 1 with CsvReadWorkbookHolder

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

the class CsvExcelReadExecutor method csvParser.

private CSVParser csvParser() throws IOException {
    CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder();
    CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat();
    if (csvReadWorkbookHolder.getMandatoryUseInputStream()) {
        return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream(), csvReadWorkbookHolder.getCharset()));
    }
    if (csvReadWorkbookHolder.getFile() != null) {
        return csvFormat.parse(new InputStreamReader(Files.newInputStream(csvReadWorkbookHolder.getFile().toPath()), csvReadWorkbookHolder.getCharset()));
    }
    return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream(), csvReadWorkbookHolder.getCharset()));
}
Also used : CsvReadWorkbookHolder(com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder) InputStreamReader(java.io.InputStreamReader) CSVFormat(org.apache.commons.csv.CSVFormat)

Example 2 with CsvReadWorkbookHolder

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

CsvReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder)2 ExcelAnalysisException (com.alibaba.excel.exception.ExcelAnalysisException)1 ReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder)1 XlsReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder)1 XlsxReadWorkbookHolder (com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder)1 InputStreamReader (java.io.InputStreamReader)1 CSVFormat (org.apache.commons.csv.CSVFormat)1