ExcelTypeEnum.java 3.6 KB
Newer Older
J
update  
jipengfei.jpf 已提交
1 2
package com.alibaba.excel.support;

庄家钜's avatar
庄家钜 已提交
3
import java.io.BufferedInputStream;
4
import java.io.File;
庄家钜's avatar
庄家钜 已提交
5
import java.io.FileInputStream;
6 7
import java.io.InputStream;

Z
zhuangjiaju 已提交
8 9
import org.apache.poi.poifs.filesystem.FileMagic;

庄家钜's avatar
庄家钜 已提交
10
import com.alibaba.excel.exception.ExcelAnalysisException;
11
import com.alibaba.excel.exception.ExcelCommonException;
庄家钜's avatar
庄家钜 已提交
12
import com.alibaba.excel.read.metadata.ReadWorkbook;
13
import com.alibaba.excel.util.StringUtils;
14

J
update  
jipengfei.jpf 已提交
15 16 17 18
/**
 * @author jipengfei
 */
public enum ExcelTypeEnum {
19 20 21 22 23 24 25 26
    /**
     * xls
     */
    XLS(".xls"),
    /**
     * xlsx
     */
    XLSX(".xlsx");
27

J
update  
jipengfei.jpf 已提交
28 29
    private String value;

30
    ExcelTypeEnum(String value) {
J
update  
jipengfei.jpf 已提交
31 32 33
        this.setValue(value);
    }

庄家钜's avatar
庄家钜 已提交
34 35 36 37 38 39 40 41 42 43
    public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) {
        ExcelTypeEnum excelType = readWorkbook.getExcelType();
        if (excelType != null) {
            return excelType;
        }
        File file = readWorkbook.getFile();
        InputStream inputStream = readWorkbook.getInputStream();
        if (file == null && inputStream == null) {
            throw new ExcelAnalysisException("File and inputStream must be a non-null.");
        }
Z
zhuangjiaju 已提交
44
        try {
45
            if (file != null) {
庄家钜's avatar
庄家钜 已提交
46 47 48
                if (!file.exists()) {
                    throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists.");
                }
49 50 51 52 53 54 55 56 57 58
                // If there is a password, use the FileMagic first
                if (!StringUtils.isEmpty(readWorkbook.getPassword())) {
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                    try {
                        return recognitionExcelType(bufferedInputStream);
                    } finally {
                        bufferedInputStream.close();
                    }
                }
                // Use the name to determine the type
庄家钜's avatar
庄家钜 已提交
59 60 61 62 63 64
                String fileName = file.getName();
                if (fileName.endsWith(XLSX.getValue())) {
                    return XLSX;
                } else if (fileName.endsWith(XLS.getValue())) {
                    return XLS;
                }
65 66 67 68 69 70 71
                if (StringUtils.isEmpty(readWorkbook.getPassword())) {
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                    try {
                        return recognitionExcelType(bufferedInputStream);
                    } finally {
                        bufferedInputStream.close();
                    }
庄家钜's avatar
庄家钜 已提交
72
                }
Z
zhuangjiaju 已提交
73
            }
庄家钜's avatar
庄家钜 已提交
74 75 76
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream);
                readWorkbook.setInputStream(inputStream);
庄家钜's avatar
庄家钜 已提交
77
            }
庄家钜's avatar
庄家钜 已提交
78 79 80 81 82 83
            return recognitionExcelType(inputStream);
        } catch (ExcelCommonException e) {
            throw e;
        } catch (ExcelAnalysisException e) {
            throw e;
        } catch (Exception e) {
84 85
            throw new ExcelCommonException(
                "Convert excel format exception.You can try specifying the 'excelType' yourself", e);
Z
zhuangjiaju 已提交
86
        }
庄家钜's avatar
庄家钜 已提交
87 88 89 90 91 92 93 94 95
    }

    private static ExcelTypeEnum recognitionExcelType(InputStream inputStream) throws Exception {
        FileMagic fileMagic = FileMagic.valueOf(inputStream);
        if (FileMagic.OLE2.equals(fileMagic)) {
            return XLS;
        }
        if (FileMagic.OOXML.equals(fileMagic)) {
            return XLSX;
庄家钜's avatar
庄家钜 已提交
96
        }
97 98
        throw new ExcelCommonException(
            "Convert excel format exception.You can try specifying the 'excelType' yourself");
Z
zhuangjiaju 已提交
99 100
    }

J
update  
jipengfei.jpf 已提交
101 102 103 104 105 106 107 108
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}