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

clevertension's avatar
clevertension 已提交
3 4 5
import java.io.InputStream;
import java.util.List;

6 7 8
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
import com.alibaba.excel.context.AnalysisContext;
clevertension's avatar
clevertension 已提交
9
import com.alibaba.excel.converters.Converter;
10 11
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
J
update  
jipengfei.jpf 已提交
12
import com.alibaba.excel.metadata.Sheet;
13
import com.alibaba.excel.parameter.AnalysisParam;
J
update  
jipengfei.jpf 已提交
14 15 16
import com.alibaba.excel.support.ExcelTypeEnum;

/**
17
 * Excel readers are all read in event mode.
J
update  
jipengfei.jpf 已提交
18 19 20 21 22 23
 *
 * @author jipengfei
 */
public class ExcelReader {

    /**
24
     * Analyser
J
update  
jipengfei.jpf 已提交
25
     */
clevertension's avatar
clevertension 已提交
26
    private ExcelAnalyser analyser;
J
update  
jipengfei.jpf 已提交
27 28

    /**
29 30
     * Create new reader
     *
clevertension's avatar
clevertension 已提交
31
     * @param in the POI filesystem that contains the Workbook stream
32
     * @param excelTypeEnum 03 or 07
clevertension's avatar
clevertension 已提交
33 34
     * @param customContent
     *        {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
35
     * @param eventListener Callback method after each row is parsed.
J
update  
jipengfei.jpf 已提交
36
     */
37
    @Deprecated
J
update  
jipengfei.jpf 已提交
38
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
clevertension's avatar
clevertension 已提交
39
                    AnalysisEventListener eventListener) {
J
update  
jipengfei.jpf 已提交
40 41 42
        this(in, excelTypeEnum, customContent, eventListener, true);
    }

43
    /**
44 45
     * Create new reader
     *
clevertension's avatar
clevertension 已提交
46 47 48
     * @param in the POI filesystem that contains the Workbook stream
     * @param customContent
     *        {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
49
     * @param eventListener Callback method after each row is parsed
50
     */
clevertension's avatar
clevertension 已提交
51 52
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener) {
        this(in, customContent, eventListener, null, true);
53 54
    }

J
update  
jipengfei.jpf 已提交
55
    /**
56 57
     * Create new reader
     *
clevertension's avatar
clevertension 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
     * @param in the POI filesystem that contains the Workbook stream
     * @param customContent
     *        {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener Callback method after each row is parsed
     */
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
                    List<Converter> converters) {
        this(in, customContent, eventListener, converters, true);
    }

    /**
     * Create new reader
     *
     * @param param old param Deprecated
72
     * @param eventListener Callback method after each row is parsed.
73 74 75 76 77 78 79
     */
    @Deprecated
    public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
        this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
    }

    /**
80 81
     * Create new reader
     *
clevertension's avatar
clevertension 已提交
82
     * @param in the POI filesystem that contains the Workbook stream
83
     * @param excelTypeEnum 03 or 07
clevertension's avatar
clevertension 已提交
84 85
     * @param customContent
     *        {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
86
     * @param eventListener Callback method after each row is parsed.
clevertension's avatar
clevertension 已提交
87 88 89 90
     * @param trim The content of the form is empty and needs to be empty. The purpose is to be
     *        fault-tolerant, because there are often table contents with spaces that can not be
     *        converted into custom types. For example: '1234 ' contain a space cannot be converted
     *        to int.
J
update  
jipengfei.jpf 已提交
91
     */
92
    @Deprecated
J
update  
jipengfei.jpf 已提交
93
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
clevertension's avatar
clevertension 已提交
94 95
                    AnalysisEventListener eventListener, boolean trim) {
        this(in, excelTypeEnum, customContent, eventListener, null, trim);
J
update  
jipengfei.jpf 已提交
96 97
    }

98
    /**
99 100
     * Create new reader
     *
101
     * @param in
clevertension's avatar
clevertension 已提交
102 103
     * @param customContent
     *        {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
104
     * @param eventListener
clevertension's avatar
clevertension 已提交
105 106 107 108
     * @param trim The content of the form is empty and needs to be empty. The purpose is to be
     *        fault-tolerant, because there are often table contents with spaces that can not be
     *        converted into custom types. For example: '1234 ' contain a space cannot be converted
     *        to int.
109
     */
clevertension's avatar
clevertension 已提交
110 111 112 113 114 115 116 117 118 119 120 121
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
                    List<Converter> converters, boolean trim) {
        this(in, ExcelTypeEnum.valueOf(in), customContent, eventListener, converters, trim);
    }

    public ExcelReader(InputStream in, Object excelTypeEnum, AnalysisEventListener<Object> eventListener,
                    boolean trim) {
        this(in, ExcelTypeEnum.valueOf(in), null, eventListener, null, trim);
    }

    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
                    AnalysisEventListener eventListener, List<Converter> converters, boolean trim) {
122
        validateParam(in, eventListener);
clevertension's avatar
clevertension 已提交
123 124 125 126 127 128 129 130 131 132 133
        analyser = new ExcelAnalyserImpl(in, excelTypeEnum, customContent, eventListener, trim);
        initConverters(analyser, converters);
    }


    private void initConverters(ExcelAnalyser analyser, List<Converter> converters) {
        if (converters != null && converters.size() > 0) {
            for (Converter c : converters) {
                analyser.getAnalysisContext().getConverterRegistryCenter().register(c);
            }
        }
134 135
    }

J
update  
jipengfei.jpf 已提交
136
    /**
137
     * Parse all sheet content by default
J
update  
jipengfei.jpf 已提交
138 139
     */
    public void read() {
clevertension's avatar
clevertension 已提交
140
        read(null, null);;
J
update  
jipengfei.jpf 已提交
141 142 143
    }

    /**
144
     * Parse the specified sheet,SheetNo start from 1
J
update  
jipengfei.jpf 已提交
145
     *
146
     * @param sheet Read sheet
J
update  
jipengfei.jpf 已提交
147 148
     */
    public void read(Sheet sheet) {
clevertension's avatar
clevertension 已提交
149
        read(sheet, null);
J
update  
jipengfei.jpf 已提交
150 151
    }

152 153 154
    /**
     * Parse the specified sheet
     *
clevertension's avatar
clevertension 已提交
155 156
     * @param sheet Read sheet
     * @param clazz object parsed into each row of value
157 158
     */
    public void read(Sheet sheet, Class<? extends BaseRowModel> clazz) {
clevertension's avatar
clevertension 已提交
159 160 161 162 163 164 165
        analyser.beforeAnalysis();
        if (sheet != null) {
            sheet.setClazz(clazz);
            analyser.analysis(sheet);
        } else {
            analyser.analysis();
        }
166 167
    }

J
update  
jipengfei.jpf 已提交
168
    /**
169
     * Parse the workBook get all sheets
J
update  
jipengfei.jpf 已提交
170
     *
171
     * @return workBook all sheets
J
update  
jipengfei.jpf 已提交
172 173 174 175 176
     */
    public List<Sheet> getSheets() {
        return analyser.getSheets();
    }

clevertension's avatar
clevertension 已提交
177 178 179 180
    public AnalysisContext getAnalysisContext() {
        return analyser.getAnalysisContext();
    }

J
update  
jipengfei.jpf 已提交
181
    /**
182
     * validate param
J
update  
jipengfei.jpf 已提交
183 184 185 186
     *
     * @param in
     * @param eventListener
     */
clevertension's avatar
clevertension 已提交
187
    private void validateParam(InputStream in, AnalysisEventListener eventListener) {
J
update  
jipengfei.jpf 已提交
188 189 190 191 192 193 194
        if (eventListener == null) {
            throw new IllegalArgumentException("AnalysisEventListener can not null");
        } else if (in == null) {
            throw new IllegalArgumentException("InputStream can not null");
        }
    }
}