ExcelTypeEnum.java 3.0 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

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

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

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

庄家钜's avatar
庄家钜 已提交
33 34 35 36 37 38 39 40 41 42
    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 已提交
43
        try {
44
            if (file != null) {
庄家钜's avatar
庄家钜 已提交
45 46 47 48 49 50 51 52 53
                if (!file.exists()) {
                    throw new ExcelAnalysisException("File " + file.getAbsolutePath() + " not exists.");
                }
                String fileName = file.getName();
                if (fileName.endsWith(XLSX.getValue())) {
                    return XLSX;
                } else if (fileName.endsWith(XLS.getValue())) {
                    return XLS;
                }
庄家钜's avatar
庄家钜 已提交
54 55
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                try {
庄家钜's avatar
庄家钜 已提交
56
                    return recognitionExcelType(bufferedInputStream);
庄家钜's avatar
庄家钜 已提交
57 58 59
                } finally {
                    bufferedInputStream.close();
                }
Z
zhuangjiaju 已提交
60
            }
庄家钜's avatar
庄家钜 已提交
61 62 63
            if (!inputStream.markSupported()) {
                inputStream = new BufferedInputStream(inputStream);
                readWorkbook.setInputStream(inputStream);
庄家钜's avatar
庄家钜 已提交
64
            }
庄家钜's avatar
庄家钜 已提交
65 66 67 68 69 70
            return recognitionExcelType(inputStream);
        } catch (ExcelCommonException e) {
            throw e;
        } catch (ExcelAnalysisException e) {
            throw e;
        } catch (Exception e) {
71 72
            throw new ExcelCommonException(
                "Convert excel format exception.You can try specifying the 'excelType' yourself", e);
Z
zhuangjiaju 已提交
73
        }
庄家钜's avatar
庄家钜 已提交
74 75 76 77 78 79 80 81 82
    }

    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
庄家钜 已提交
83
        }
84 85
        throw new ExcelCommonException(
            "Convert excel format exception.You can try specifying the 'excelType' yourself");
Z
zhuangjiaju 已提交
86 87
    }

J
update  
jipengfei.jpf 已提交
88 89 90 91 92 93 94 95
    public String getValue() {
        return value;
    }

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