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

clevertension's avatar
clevertension 已提交
3
import java.io.InputStream;
4
import java.util.ArrayList;
庄家钜's avatar
庄家钜 已提交
5
import java.util.Arrays;
clevertension's avatar
clevertension 已提交
6 7
import java.util.List;

Z
zhuangjiaju 已提交
8 9 10
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

11 12
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
庄家钜's avatar
庄家钜 已提交
13
import com.alibaba.excel.analysis.ExcelReadExecutor;
14
import com.alibaba.excel.cache.MapCache;
15 16
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
17
import com.alibaba.excel.metadata.Sheet;
18
import com.alibaba.excel.parameter.AnalysisParam;
19 20 21
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;
J
update  
jipengfei.jpf 已提交
22 23 24
import com.alibaba.excel.support.ExcelTypeEnum;

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

    /**
33
     * Analyser
J
update  
jipengfei.jpf 已提交
34
     */
Z
zhuangjiaju 已提交
35 36
    private ExcelAnalyser excelAnalyser;

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

56
    /**
57 58
     * Create new reader
     *
Z
zhuangjiaju 已提交
59 60
     * @param in
     *            the POI filesystem that contains the Workbook stream
clevertension's avatar
clevertension 已提交
61
     * @param customContent
Z
zhuangjiaju 已提交
62 63 64
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     *            Callback method after each row is parsed
65
     * @deprecated please use {@link EasyExcelFactory#read()} build 'ExcelReader'
66
     */
67
    @Deprecated
clevertension's avatar
clevertension 已提交
68
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener) {
69
        this(in, customContent, eventListener, true);
clevertension's avatar
clevertension 已提交
70 71 72 73 74
    }

    /**
     * Create new reader
     *
Z
zhuangjiaju 已提交
75 76 77 78
     * @param param
     *            old param Deprecated
     * @param eventListener
     *            Callback method after each row is parsed.
79
     * @deprecated please use {@link EasyExcelFactory#read()} build 'ExcelReader'
80 81 82 83 84 85 86
     */
    @Deprecated
    public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
        this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
    }

    /**
87 88
     * Create new reader
     *
Z
zhuangjiaju 已提交
89
     * @param in
clevertension's avatar
clevertension 已提交
90
     * @param customContent
Z
zhuangjiaju 已提交
91 92 93 94 95 96
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
     * @param eventListener
     * @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.
97
     * @deprecated please use {@link EasyExcelFactory#read()} build 'ExcelReader'
J
update  
jipengfei.jpf 已提交
98
     */
99
    @Deprecated
100 101
    public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener, boolean trim) {
        this(in, null, customContent, eventListener, trim);
J
update  
jipengfei.jpf 已提交
102 103
    }

104
    /**
105 106
     * Create new reader
     *
107
     * @param in
108 109 110
     *            the POI filesystem that contains the Workbook stream
     * @param excelTypeEnum
     *            03 or 07
clevertension's avatar
clevertension 已提交
111
     * @param customContent
Z
zhuangjiaju 已提交
112
     *            {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
113
     * @param eventListener
114
     *            Callback method after each row is parsed.
Z
zhuangjiaju 已提交
115 116 117 118
     * @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.
119
     * @deprecated please use {@link EasyExcelFactory#read()} build 'ExcelReader'
120
     */
121
    @Deprecated
clevertension's avatar
clevertension 已提交
122
    public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        AnalysisEventListener eventListener, boolean trim) {
        ReadWorkbook readWorkbook = new ReadWorkbook();
        readWorkbook.setInputStream(in);
        readWorkbook.setExcelType(excelTypeEnum);
        readWorkbook.setCustomObject(customContent);
        if (eventListener != null) {
            List<ReadListener> customReadListenerList = new ArrayList<ReadListener>();
            customReadListenerList.add(eventListener);
            readWorkbook.setCustomReadListenerList(customReadListenerList);
        }
        readWorkbook.setAutoTrim(trim);
        readWorkbook.setAutoCloseStream(Boolean.FALSE);
        readWorkbook.setMandatoryUseInputStream(Boolean.TRUE);
        readWorkbook.setReadCache(new MapCache());
        readWorkbook.setConvertAllFiled(Boolean.FALSE);
Z
zhuangjiaju 已提交
138
        readWorkbook.setDefaultReturnMap(Boolean.FALSE);
139
        excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
clevertension's avatar
clevertension 已提交
140 141
    }

142 143
    public ExcelReader(ReadWorkbook readWorkbook) {
        excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
144 145
    }

J
update  
jipengfei.jpf 已提交
146
    /**
147
     * Parse all sheet content by default
庄家钜's avatar
庄家钜 已提交
148 149
     *
     * @deprecated lease use {@link #readAll()}
J
update  
jipengfei.jpf 已提交
150
     */
庄家钜's avatar
庄家钜 已提交
151
    @Deprecated
J
update  
jipengfei.jpf 已提交
152
    public void read() {
庄家钜's avatar
庄家钜 已提交
153 154 155 156 157 158 159 160
        readAll();
    }

    /***
     * Parse all sheet content by default
     */
    public void readAll() {
        excelAnalyser.analysis(null, Boolean.TRUE);
J
update  
jipengfei.jpf 已提交
161 162
    }

163
    /**
庄家钜's avatar
庄家钜 已提交
164
     * Parse the specified sheet,SheetNo start from 0
165 166 167 168
     *
     * @param readSheet
     *            Read sheet
     */
庄家钜's avatar
庄家钜 已提交
169 170 171 172 173 174 175 176 177 178 179 180
    public ExcelReader read(ReadSheet... readSheet) {
        return read(Arrays.asList(readSheet));
    }

    /**
     * Read multiple sheets.
     *
     * @param readSheetList
     * @return
     */
    public ExcelReader read(List<ReadSheet> readSheetList) {
        excelAnalyser.analysis(readSheetList, Boolean.FALSE);
181 182 183
        return this;
    }

J
update  
jipengfei.jpf 已提交
184
    /**
185
     * Parse the specified sheet,SheetNo start from 1
J
update  
jipengfei.jpf 已提交
186
     *
Z
zhuangjiaju 已提交
187 188
     * @param sheet
     *            Read sheet
庄家钜's avatar
庄家钜 已提交
189
     * @deprecated please us {@link #read(ReadSheet...)}
J
update  
jipengfei.jpf 已提交
190
     */
191
    @Deprecated
J
update  
jipengfei.jpf 已提交
192
    public void read(Sheet sheet) {
193 194 195 196 197 198 199 200 201 202
        ReadSheet readSheet = null;
        if (sheet != null) {
            readSheet = new ReadSheet();
            readSheet.setSheetNo(sheet.getSheetNo() - 1);
            readSheet.setSheetName(sheet.getSheetName());
            readSheet.setClazz(sheet.getClazz());
            readSheet.setHead(sheet.getHead());
            readSheet.setHeadRowNumber(sheet.getHeadLineMun());
        }
        read(readSheet);
J
update  
jipengfei.jpf 已提交
203 204
    }

205 206 207
    /**
     * Parse the specified sheet
     *
Z
zhuangjiaju 已提交
208 209 210 211 212 213
     * @param sheet
     *            Read sheet
     * @param clazz
     *            object parsed into each row of value
     *
     * @deprecated Set the class in the sheet before read
214
     */
Z
zhuangjiaju 已提交
215
    @Deprecated
Z
zhuangjiaju 已提交
216
    public void read(Sheet sheet, Class clazz) {
clevertension's avatar
clevertension 已提交
217 218 219
        if (sheet != null) {
            sheet.setClazz(clazz);
        }
Z
zhuangjiaju 已提交
220 221 222 223 224
        read(sheet);
    }

    /**
     * Context for the entire execution process
Z
zhuangjiaju 已提交
225
     *
Z
zhuangjiaju 已提交
226 227 228 229 230 231 232 233
     * @return
     */
    public AnalysisContext analysisContext() {
        return excelAnalyser.analysisContext();
    }

    /**
     * Current executor
Z
zhuangjiaju 已提交
234
     *
Z
zhuangjiaju 已提交
235 236
     * @return
     */
庄家钜's avatar
庄家钜 已提交
237
    public ExcelReadExecutor excelExecutor() {
Z
zhuangjiaju 已提交
238
        return excelAnalyser.excelExecutor();
239 240
    }

J
update  
jipengfei.jpf 已提交
241
    /**
242
     * Parse the workBook get all sheets
J
update  
jipengfei.jpf 已提交
243
     *
244
     * @return workBook all sheets
Z
zhuangjiaju 已提交
245 246
     *
     * @deprecated please use {@link #excelExecutor()}
J
update  
jipengfei.jpf 已提交
247
     */
Z
zhuangjiaju 已提交
248
    @Deprecated
J
update  
jipengfei.jpf 已提交
249
    public List<Sheet> getSheets() {
250 251 252 253 254 255 256 257 258 259 260
        List<ReadSheet> sheetList = excelExecutor().sheetList();
        List<Sheet> sheets = new ArrayList<Sheet>();
        if (sheetList == null || sheetList.isEmpty()) {
            return sheets;
        }
        for (ReadSheet readSheet : sheetList) {
            Sheet sheet = new Sheet(readSheet.getSheetNo() + 1);
            sheet.setSheetName(readSheet.getSheetName());
            sheets.add(sheet);
        }
        return sheets;
J
update  
jipengfei.jpf 已提交
261 262
    }

Z
zhuangjiaju 已提交
263
    /**
Z
zhuangjiaju 已提交
264
     *
Z
zhuangjiaju 已提交
265 266 267 268
     * @return
     * @deprecated please use {@link #analysisContext()}
     */
    @Deprecated
clevertension's avatar
clevertension 已提交
269
    public AnalysisContext getAnalysisContext() {
Z
zhuangjiaju 已提交
270
        return analysisContext();
clevertension's avatar
clevertension 已提交
271 272
    }

J
update  
jipengfei.jpf 已提交
273
    /**
Z
zhuangjiaju 已提交
274
     * Complete the entire read file.Release the cache and close stream.
J
update  
jipengfei.jpf 已提交
275
     */
Z
zhuangjiaju 已提交
276
    public void finish() {
277 278 279
        if (excelAnalyser != null) {
            excelAnalyser.finish();
        }
Z
zhuangjiaju 已提交
280 281 282 283
    }

    /**
     * Prevents calls to {@link #finish} from freeing the cache
Z
zhuangjiaju 已提交
284
     *
Z
zhuangjiaju 已提交
285 286 287 288
     */
    @Override
    protected void finalize() {
        try {
289
            finish();
Z
zhuangjiaju 已提交
290
        } catch (Throwable e) {
Z
zhuangjiaju 已提交
291 292 293 294
            LOGGER.warn("Destroy object failed", e);
        }
    }

J
update  
jipengfei.jpf 已提交
295
}