Search in sources :

Example 1 with ExcelTypeEnum

use of com.alibaba.excel.support.ExcelTypeEnum in project boot-admin by hb0730.

the class ExcelUtils method getOutputStream.

/**
 * 获取output流
 *
 * @param excelProperties excel配置
 * @return 输出流
 */
@NonNull
public static OutputStream getOutputStream(@NonNull ExcelProperties excelProperties) throws FileNotFoundException {
    String path = excelProperties.getPath();
    String fileName = excelProperties.getFileName();
    ExcelTypeEnum excelType = excelProperties.getExcelType();
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
    String sb = path + "/" + fileName + "-" + format.format(new Date()) + "." + excelType.getValue();
    return new FileOutputStream(sb);
}
Also used : ExcelTypeEnum(com.alibaba.excel.support.ExcelTypeEnum) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) NonNull(org.springframework.lang.NonNull)

Example 2 with ExcelTypeEnum

use of com.alibaba.excel.support.ExcelTypeEnum 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

ExcelTypeEnum (com.alibaba.excel.support.ExcelTypeEnum)2 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 InputStream (java.io.InputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1 NonNull (org.springframework.lang.NonNull)1