ExcelReader.java 2.9 KB
Newer Older
J
update  
jipengfei.jpf 已提交
1 2
package com.alibaba.excel;

3 4 5 6 7
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
J
update  
jipengfei.jpf 已提交
8
import com.alibaba.excel.metadata.Sheet;
9
import com.alibaba.excel.parameter.AnalysisParam;
J
update  
jipengfei.jpf 已提交
10 11
import com.alibaba.excel.support.ExcelTypeEnum;

12 13 14
import java.io.InputStream;
import java.util.List;

J
update  
jipengfei.jpf 已提交
15
/**
16
 * Excel thread unsafe
J
update  
jipengfei.jpf 已提交
17 18 19 20 21 22
 *
 * @author jipengfei
 */
public class ExcelReader {

    /**
23
     * analyser
J
update  
jipengfei.jpf 已提交
24 25 26 27
     */
    private ExcelAnalyser analyser = new ExcelAnalyserImpl();

    /**
28 29 30 31 32
     * @param in
     * @param excelTypeEnum 0307
     * @param customContent {@link AnalysisEventListener#invoke(Object, AnalysisContext)
     *                      }AnalysisContext
     * @param eventListener
J
update  
jipengfei.jpf 已提交
33 34 35 36 37 38 39
     */
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
                       AnalysisEventListener eventListener) {
        this(in, excelTypeEnum, customContent, eventListener, true);
    }

    /**
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
     * old 1.1.0
     * @param param
     * @param eventListener
     */
    @Deprecated
    public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
        this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
    }

    /**
     * @param in
     * @param excelTypeEnum 03 07
     * @param customContent {@link AnalysisEventListener#invoke(Object, AnalysisContext)
     *                      }AnalysisContext
     * @param eventListener
     * @param trim
J
update  
jipengfei.jpf 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
     */
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
                       AnalysisEventListener eventListener, boolean trim) {
        validateParam(in, excelTypeEnum, eventListener);
        analyser.init(in, excelTypeEnum, customContent, eventListener, trim);
    }

    /**
     */
    public void read() {
        analyser.analysis();
    }

    /**
     *
71
     * @param sheet
J
update  
jipengfei.jpf 已提交
72 73 74 75 76
     */
    public void read(Sheet sheet) {
        analyser.analysis(sheet);
    }

77 78 79 80 81 82
    @Deprecated
    public void read(Sheet sheet,Class<? extends BaseRowModel> clazz){
        sheet.setClazz(clazz);
        analyser.analysis(sheet);
    }

J
update  
jipengfei.jpf 已提交
83 84
    /**
     *
85
     * @return
J
update  
jipengfei.jpf 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
     */
    public List<Sheet> getSheets() {
        return analyser.getSheets();
    }

    /**
     *
     * @param in
     * @param excelTypeEnum
     * @param eventListener
     */
    private void validateParam(InputStream in, ExcelTypeEnum excelTypeEnum, AnalysisEventListener eventListener) {
        if (eventListener == null) {
            throw new IllegalArgumentException("AnalysisEventListener can not null");
        } else if (in == null) {
            throw new IllegalArgumentException("InputStream can not null");
        } else if (excelTypeEnum == null) {
            throw new IllegalArgumentException("excelTypeEnum can not null");
        }
    }
}