ExcelReader.java 9.4 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;
Z
zhuangjiaju 已提交
17
import com.alibaba.excel.exception.ExcelAnalysisException;
18
import com.alibaba.excel.metadata.Sheet;
19
import com.alibaba.excel.parameter.AnalysisParam;
20 21 22
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 已提交
23 24 25
import com.alibaba.excel.support.ExcelTypeEnum;

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * Read multiple sheets.
     *
     * @param readSheetList
     * @return
     */
    public ExcelReader read(List<ReadSheet> readSheetList) {
184
        checkFinished();
庄家钜's avatar
庄家钜 已提交
185
        excelAnalyser.analysis(readSheetList, Boolean.FALSE);
186 187 188
        return this;
    }

J
update  
jipengfei.jpf 已提交
189
    /**
190
     * Parse the specified sheet,SheetNo start from 1
J
update  
jipengfei.jpf 已提交
191
     *
Z
zhuangjiaju 已提交
192 193
     * @param sheet
     *            Read sheet
庄家钜's avatar
庄家钜 已提交
194
     * @deprecated please us {@link #read(ReadSheet...)}
J
update  
jipengfei.jpf 已提交
195
     */
196
    @Deprecated
J
update  
jipengfei.jpf 已提交
197
    public void read(Sheet sheet) {
198 199 200 201 202 203 204 205 206 207
        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 已提交
208 209
    }

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

    /**
     * Context for the entire execution process
Z
zhuangjiaju 已提交
230
     *
Z
zhuangjiaju 已提交
231 232 233 234 235 236 237 238 239
     * @return
     */
    public AnalysisContext analysisContext() {
        checkFinished();
        return excelAnalyser.analysisContext();
    }

    /**
     * Current executor
Z
zhuangjiaju 已提交
240
     *
Z
zhuangjiaju 已提交
241 242
     * @return
     */
庄家钜's avatar
庄家钜 已提交
243
    public ExcelReadExecutor excelExecutor() {
Z
zhuangjiaju 已提交
244 245
        checkFinished();
        return excelAnalyser.excelExecutor();
246 247
    }

J
update  
jipengfei.jpf 已提交
248
    /**
249
     * Parse the workBook get all sheets
J
update  
jipengfei.jpf 已提交
250
     *
251
     * @return workBook all sheets
Z
zhuangjiaju 已提交
252 253
     *
     * @deprecated please use {@link #excelExecutor()}
J
update  
jipengfei.jpf 已提交
254
     */
Z
zhuangjiaju 已提交
255
    @Deprecated
J
update  
jipengfei.jpf 已提交
256
    public List<Sheet> getSheets() {
257 258 259 260 261 262 263 264 265 266 267
        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 已提交
268 269
    }

Z
zhuangjiaju 已提交
270
    /**
Z
zhuangjiaju 已提交
271
     *
Z
zhuangjiaju 已提交
272 273 274 275
     * @return
     * @deprecated please use {@link #analysisContext()}
     */
    @Deprecated
clevertension's avatar
clevertension 已提交
276
    public AnalysisContext getAnalysisContext() {
Z
zhuangjiaju 已提交
277
        return analysisContext();
clevertension's avatar
clevertension 已提交
278 279
    }

J
update  
jipengfei.jpf 已提交
280
    /**
Z
zhuangjiaju 已提交
281
     * Complete the entire read file.Release the cache and close stream.
J
update  
jipengfei.jpf 已提交
282
     */
Z
zhuangjiaju 已提交
283 284 285 286 287 288 289 290 291 292
    public void finish() {
        if (finished) {
            return;
        }
        finished = true;
        excelAnalyser.finish();
    }

    /**
     * Prevents calls to {@link #finish} from freeing the cache
Z
zhuangjiaju 已提交
293
     *
Z
zhuangjiaju 已提交
294 295 296 297 298 299 300 301
     */
    @Override
    protected void finalize() {
        if (finished) {
            return;
        }
        try {
            excelAnalyser.finish();
Z
zhuangjiaju 已提交
302
        } catch (Throwable e) {
Z
zhuangjiaju 已提交
303 304 305 306 307 308 309 310
            LOGGER.warn("Destroy object failed", e);
        }
    }

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