ExcelReader.java 9.2 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;
clevertension's avatar
clevertension 已提交
5 6
import java.util.List;

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

10 11
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
Z
zhuangjiaju 已提交
12
import com.alibaba.excel.analysis.ExcelExecutor;
13
import com.alibaba.excel.cache.MapCache;
14 15
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
Z
zhuangjiaju 已提交
16
import com.alibaba.excel.exception.ExcelAnalysisException;
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 37
    private ExcelAnalyser excelAnalyser;

    private boolean finished = false;
J
update  
jipengfei.jpf 已提交
38 39

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

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

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

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

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

144 145
    public ExcelReader(ReadWorkbook readWorkbook) {
        excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
146 147
    }

J
update  
jipengfei.jpf 已提交
148
    /**
149
     * Parse all sheet content by default
J
update  
jipengfei.jpf 已提交
150 151
     */
    public void read() {
Z
zhuangjiaju 已提交
152 153 154 155 156
        ExcelExecutor excelExecutor = excelAnalyser.excelExecutor();
        if (excelExecutor.sheetList().isEmpty()) {
            LOGGER.warn("Excel doesn't have any sheets.");
            return;
        }
157 158
        for (ReadSheet readSheet : excelExecutor.sheetList()) {
            read(readSheet);
Z
zhuangjiaju 已提交
159
        }
J
update  
jipengfei.jpf 已提交
160 161
    }

162 163 164 165 166 167 168 169 170 171 172 173
    /**
     * Parse the specified sheet,SheetNo start from 1
     *
     * @param readSheet
     *            Read sheet
     */
    public ExcelReader read(ReadSheet readSheet) {
        checkFinished();
        excelAnalyser.analysis(readSheet);
        return this;
    }

J
update  
jipengfei.jpf 已提交
174
    /**
175
     * Parse the specified sheet,SheetNo start from 1
J
update  
jipengfei.jpf 已提交
176
     *
Z
zhuangjiaju 已提交
177 178
     * @param sheet
     *            Read sheet
179
     * @deprecated please us {@link #read(ReadSheet)}
J
update  
jipengfei.jpf 已提交
180
     */
181
    @Deprecated
J
update  
jipengfei.jpf 已提交
182
    public void read(Sheet sheet) {
183 184 185 186 187 188 189 190 191 192
        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 已提交
193 194
    }

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

    /**
     * Context for the entire execution process
Z
zhuangjiaju 已提交
215
     *
Z
zhuangjiaju 已提交
216 217 218 219 220 221 222 223 224
     * @return
     */
    public AnalysisContext analysisContext() {
        checkFinished();
        return excelAnalyser.analysisContext();
    }

    /**
     * Current executor
Z
zhuangjiaju 已提交
225
     *
Z
zhuangjiaju 已提交
226 227 228 229 230
     * @return
     */
    public ExcelExecutor excelExecutor() {
        checkFinished();
        return excelAnalyser.excelExecutor();
231 232
    }

J
update  
jipengfei.jpf 已提交
233
    /**
234
     * Parse the workBook get all sheets
J
update  
jipengfei.jpf 已提交
235
     *
236
     * @return workBook all sheets
Z
zhuangjiaju 已提交
237 238
     *
     * @deprecated please use {@link #excelExecutor()}
J
update  
jipengfei.jpf 已提交
239
     */
Z
zhuangjiaju 已提交
240
    @Deprecated
J
update  
jipengfei.jpf 已提交
241
    public List<Sheet> getSheets() {
242 243 244 245 246 247 248 249 250 251 252
        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 已提交
253 254
    }

Z
zhuangjiaju 已提交
255
    /**
Z
zhuangjiaju 已提交
256
     *
Z
zhuangjiaju 已提交
257 258 259 260
     * @return
     * @deprecated please use {@link #analysisContext()}
     */
    @Deprecated
clevertension's avatar
clevertension 已提交
261
    public AnalysisContext getAnalysisContext() {
Z
zhuangjiaju 已提交
262
        return analysisContext();
clevertension's avatar
clevertension 已提交
263 264
    }

J
update  
jipengfei.jpf 已提交
265
    /**
Z
zhuangjiaju 已提交
266
     * Complete the entire read file.Release the cache and close stream.
J
update  
jipengfei.jpf 已提交
267
     */
Z
zhuangjiaju 已提交
268 269 270 271 272 273 274 275 276 277
    public void finish() {
        if (finished) {
            return;
        }
        finished = true;
        excelAnalyser.finish();
    }

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

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