ExcelReader.java 8.2 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;

Z
zhuangjiaju 已提交
6 7 8
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

9 10
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
Z
zhuangjiaju 已提交
11
import com.alibaba.excel.analysis.ExcelExecutor;
12
import com.alibaba.excel.context.AnalysisContext;
clevertension's avatar
clevertension 已提交
13
import com.alibaba.excel.converters.Converter;
14
import com.alibaba.excel.event.AnalysisEventListener;
Z
zhuangjiaju 已提交
15
import com.alibaba.excel.exception.ExcelAnalysisException;
Z
zhuangjiaju 已提交
16
import com.alibaba.excel.write.metadata.Sheet;
17
import com.alibaba.excel.parameter.AnalysisParam;
J
update  
jipengfei.jpf 已提交
18 19 20
import com.alibaba.excel.support.ExcelTypeEnum;

/**
21
 * Excel readers are all read in event mode.
J
update  
jipengfei.jpf 已提交
22 23 24 25
 *
 * @author jipengfei
 */
public class ExcelReader {
Z
zhuangjiaju 已提交
26
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelReader.class);
J
update  
jipengfei.jpf 已提交
27 28

    /**
29
     * Analyser
J
update  
jipengfei.jpf 已提交
30
     */
Z
zhuangjiaju 已提交
31 32 33
    private ExcelAnalyser excelAnalyser;

    private boolean finished = false;
J
update  
jipengfei.jpf 已提交
34 35

    /**
36 37
     * Create new reader
     *
Z
zhuangjiaju 已提交
38 39 40 41
     * @param in
     *            the POI filesystem that contains the Workbook stream
     * @param excelTypeEnum
     *            03 or 07
clevertension's avatar
clevertension 已提交
42
     * @param customContent
Z
zhuangjiaju 已提交
43 44 45
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     *            Callback method after each row is parsed.
J
update  
jipengfei.jpf 已提交
46
     */
47
    @Deprecated
J
update  
jipengfei.jpf 已提交
48
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
Z
zhuangjiaju 已提交
49
        AnalysisEventListener eventListener) {
J
update  
jipengfei.jpf 已提交
50 51 52
        this(in, excelTypeEnum, customContent, eventListener, true);
    }

53
    /**
54 55
     * Create new reader
     *
Z
zhuangjiaju 已提交
56 57
     * @param in
     *            the POI filesystem that contains the Workbook stream
clevertension's avatar
clevertension 已提交
58
     * @param customContent
Z
zhuangjiaju 已提交
59 60 61
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     *            Callback method after each row is parsed
62
     */
clevertension's avatar
clevertension 已提交
63 64
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener) {
        this(in, customContent, eventListener, null, true);
65 66
    }

J
update  
jipengfei.jpf 已提交
67
    /**
68 69
     * Create new reader
     *
Z
zhuangjiaju 已提交
70 71
     * @param in
     *            the POI filesystem that contains the Workbook stream
clevertension's avatar
clevertension 已提交
72
     * @param customContent
Z
zhuangjiaju 已提交
73 74 75
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     *            Callback method after each row is parsed
clevertension's avatar
clevertension 已提交
76 77
     */
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
Z
zhuangjiaju 已提交
78
        List<Converter> converters) {
clevertension's avatar
clevertension 已提交
79 80 81 82 83 84
        this(in, customContent, eventListener, converters, true);
    }

    /**
     * Create new reader
     *
Z
zhuangjiaju 已提交
85 86 87 88
     * @param param
     *            old param Deprecated
     * @param eventListener
     *            Callback method after each row is parsed.
89 90 91 92 93 94 95
     */
    @Deprecated
    public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
        this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
    }

    /**
96 97
     * Create new reader
     *
Z
zhuangjiaju 已提交
98 99 100 101
     * @param in
     *            the POI filesystem that contains the Workbook stream
     * @param excelTypeEnum
     *            03 or 07
clevertension's avatar
clevertension 已提交
102
     * @param customContent
Z
zhuangjiaju 已提交
103 104 105 106 107 108 109
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     *            Callback method after each row is parsed.
     * @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 已提交
110
     */
111
    @Deprecated
J
update  
jipengfei.jpf 已提交
112
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
Z
zhuangjiaju 已提交
113
        AnalysisEventListener eventListener, boolean trim) {
clevertension's avatar
clevertension 已提交
114
        this(in, excelTypeEnum, customContent, eventListener, null, trim);
J
update  
jipengfei.jpf 已提交
115 116
    }

117
    /**
118 119
     * Create new reader
     *
120
     * @param in
clevertension's avatar
clevertension 已提交
121
     * @param customContent
Z
zhuangjiaju 已提交
122
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
123
     * @param eventListener
Z
zhuangjiaju 已提交
124 125 126 127
     * @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.
128
     */
clevertension's avatar
clevertension 已提交
129
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
Z
zhuangjiaju 已提交
130
        List<Converter> converters, boolean trim) {
clevertension's avatar
clevertension 已提交
131 132 133 134
        this(in, ExcelTypeEnum.valueOf(in), customContent, eventListener, converters, trim);
    }

    public ExcelReader(InputStream in, Object excelTypeEnum, AnalysisEventListener<Object> eventListener,
Z
zhuangjiaju 已提交
135
        boolean trim) {
clevertension's avatar
clevertension 已提交
136 137 138 139
        this(in, ExcelTypeEnum.valueOf(in), null, eventListener, null, trim);
    }

    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
Z
zhuangjiaju 已提交
140
        AnalysisEventListener eventListener, List<Converter> converters, boolean trim) {
141
        validateParam(in, eventListener);
clevertension's avatar
clevertension 已提交
142 143 144 145 146 147 148 149 150 151
        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);
            }
        }
152 153
    }

J
update  
jipengfei.jpf 已提交
154
    /**
155
     * Parse all sheet content by default
J
update  
jipengfei.jpf 已提交
156 157
     */
    public void read() {
Z
zhuangjiaju 已提交
158 159 160 161 162 163 164 165
        ExcelExecutor excelExecutor = excelAnalyser.excelExecutor();
        if (excelExecutor.sheetList().isEmpty()) {
            LOGGER.warn("Excel doesn't have any sheets.");
            return;
        }
        for (Sheet sheet : excelExecutor.sheetList()) {
            read(sheet);
        }
J
update  
jipengfei.jpf 已提交
166 167 168
    }

    /**
169
     * Parse the specified sheet,SheetNo start from 1
J
update  
jipengfei.jpf 已提交
170
     *
Z
zhuangjiaju 已提交
171 172
     * @param sheet
     *            Read sheet
J
update  
jipengfei.jpf 已提交
173 174
     */
    public void read(Sheet sheet) {
Z
zhuangjiaju 已提交
175 176
        checkFinished();
        excelAnalyser.analysis(sheet);
J
update  
jipengfei.jpf 已提交
177 178
    }

179 180 181
    /**
     * Parse the specified sheet
     *
Z
zhuangjiaju 已提交
182 183 184 185 186 187
     * @param sheet
     *            Read sheet
     * @param clazz
     *            object parsed into each row of value
     *
     * @deprecated Set the class in the sheet before read
188
     */
Z
zhuangjiaju 已提交
189
    @Deprecated
Z
zhuangjiaju 已提交
190
    public void read(Sheet sheet, Class clazz) {
clevertension's avatar
clevertension 已提交
191 192 193
        if (sheet != null) {
            sheet.setClazz(clazz);
        }
Z
zhuangjiaju 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        read(sheet);
    }

    /**
     * Context for the entire execution process
     * 
     * @return
     */
    public AnalysisContext analysisContext() {
        checkFinished();
        return excelAnalyser.analysisContext();
    }

    /**
     * Current executor
     * 
     * @return
     */
    public ExcelExecutor excelExecutor() {
        checkFinished();
        return excelAnalyser.excelExecutor();
215 216
    }

J
update  
jipengfei.jpf 已提交
217
    /**
218
     * Parse the workBook get all sheets
J
update  
jipengfei.jpf 已提交
219
     *
220
     * @return workBook all sheets
Z
zhuangjiaju 已提交
221 222
     *
     * @deprecated please use {@link #excelExecutor()}
J
update  
jipengfei.jpf 已提交
223
     */
Z
zhuangjiaju 已提交
224
    @Deprecated
J
update  
jipengfei.jpf 已提交
225
    public List<Sheet> getSheets() {
Z
zhuangjiaju 已提交
226
        return excelExecutor().sheetList();
J
update  
jipengfei.jpf 已提交
227 228
    }

Z
zhuangjiaju 已提交
229 230 231 232 233 234
    /**
     * 
     * @return
     * @deprecated please use {@link #analysisContext()}
     */
    @Deprecated
clevertension's avatar
clevertension 已提交
235
    public AnalysisContext getAnalysisContext() {
Z
zhuangjiaju 已提交
236
        return analysisContext();
clevertension's avatar
clevertension 已提交
237 238
    }

J
update  
jipengfei.jpf 已提交
239
    /**
Z
zhuangjiaju 已提交
240
     * Complete the entire read file.Release the cache and close stream.
J
update  
jipengfei.jpf 已提交
241
     */
Z
zhuangjiaju 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    public void finish() {
        if (finished) {
            return;
        }
        finished = true;
        excelAnalyser.finish();
    }

    /**
     * Prevents calls to {@link #finish} from freeing the cache
     * 
     * @throws Throwable
     */
    @Override
    protected void finalize() {
        if (finished) {
            return;
        }
        try {
            excelAnalyser.finish();
        } catch (Exception e) {
            LOGGER.warn("Destroy object failed", e);
        }
    }

    private void checkFinished() {
        if (finished) {
            throw new ExcelAnalysisException("Can not use a finished reader.");
        }
J
update  
jipengfei.jpf 已提交
271 272
    }
}