Search in sources :

Example 1 with XlsReadContext

use of com.alibaba.excel.context.xls.XlsReadContext in project easyexcel by alibaba.

the class ExcelAnalyserImpl method choiceExcelExecutor.

private void choiceExcelExecutor(ReadWorkbook readWorkbook) throws Exception {
    ExcelTypeEnum excelType = ExcelTypeEnum.valueOf(readWorkbook);
    switch(excelType) {
        case XLS:
            POIFSFileSystem poifsFileSystem;
            if (readWorkbook.getFile() != null) {
                poifsFileSystem = new POIFSFileSystem(readWorkbook.getFile());
            } else {
                poifsFileSystem = new POIFSFileSystem(readWorkbook.getInputStream());
            }
            // So in encrypted excel, it looks like XLS but it's actually XLSX
            if (poifsFileSystem.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
                InputStream decryptedStream = null;
                try {
                    decryptedStream = DocumentFactoryHelper.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword());
                    XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX);
                    analysisContext = xlsxReadContext;
                    excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, decryptedStream);
                    return;
                } finally {
                    IOUtils.closeQuietly(decryptedStream);
                    // as we processed the full stream already, we can close the filesystem here
                    // otherwise file handles are leaked
                    poifsFileSystem.close();
                }
            }
            if (readWorkbook.getPassword() != null) {
                Biff8EncryptionKey.setCurrentUserPassword(readWorkbook.getPassword());
            }
            XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook, ExcelTypeEnum.XLS);
            xlsReadContext.xlsReadWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
            analysisContext = xlsReadContext;
            excelReadExecutor = new XlsSaxAnalyser(xlsReadContext);
            break;
        case XLSX:
            XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX);
            analysisContext = xlsxReadContext;
            excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, null);
            break;
        case CSV:
            CsvReadContext csvReadContext = new DefaultCsvReadContext(readWorkbook, ExcelTypeEnum.CSV);
            analysisContext = csvReadContext;
            excelReadExecutor = new CsvExcelReadExecutor(csvReadContext);
            break;
        default:
            break;
    }
}
Also used : DefaultCsvReadContext(com.alibaba.excel.context.csv.DefaultCsvReadContext) ExcelTypeEnum(com.alibaba.excel.support.ExcelTypeEnum) XlsSaxAnalyser(com.alibaba.excel.analysis.v03.XlsSaxAnalyser) CsvExcelReadExecutor(com.alibaba.excel.analysis.csv.CsvExcelReadExecutor) DefaultXlsReadContext(com.alibaba.excel.context.xls.DefaultXlsReadContext) XlsReadContext(com.alibaba.excel.context.xls.XlsReadContext) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) InputStream(java.io.InputStream) XlsxSaxAnalyser(com.alibaba.excel.analysis.v07.XlsxSaxAnalyser) DefaultXlsReadContext(com.alibaba.excel.context.xls.DefaultXlsReadContext) DefaultXlsxReadContext(com.alibaba.excel.context.xlsx.DefaultXlsxReadContext) DefaultCsvReadContext(com.alibaba.excel.context.csv.DefaultCsvReadContext) CsvReadContext(com.alibaba.excel.context.csv.CsvReadContext) DefaultXlsxReadContext(com.alibaba.excel.context.xlsx.DefaultXlsxReadContext) XlsxReadContext(com.alibaba.excel.context.xlsx.XlsxReadContext)

Aggregations

CsvExcelReadExecutor (com.alibaba.excel.analysis.csv.CsvExcelReadExecutor)1 XlsSaxAnalyser (com.alibaba.excel.analysis.v03.XlsSaxAnalyser)1 XlsxSaxAnalyser (com.alibaba.excel.analysis.v07.XlsxSaxAnalyser)1 CsvReadContext (com.alibaba.excel.context.csv.CsvReadContext)1 DefaultCsvReadContext (com.alibaba.excel.context.csv.DefaultCsvReadContext)1 DefaultXlsReadContext (com.alibaba.excel.context.xls.DefaultXlsReadContext)1 XlsReadContext (com.alibaba.excel.context.xls.XlsReadContext)1 DefaultXlsxReadContext (com.alibaba.excel.context.xlsx.DefaultXlsxReadContext)1 XlsxReadContext (com.alibaba.excel.context.xlsx.XlsxReadContext)1 ExcelTypeEnum (com.alibaba.excel.support.ExcelTypeEnum)1 InputStream (java.io.InputStream)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1