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 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);
        excelAnalyser = new ExcelAnalyserImpl(readWorkbook);
clevertension's avatar
clevertension 已提交
141 142
    }

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

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

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

194 195 196
    /**
     * Parse the specified sheet
     *
Z
zhuangjiaju 已提交
197 198 199 200 201 202
     * @param sheet
     *            Read sheet
     * @param clazz
     *            object parsed into each row of value
     *
     * @deprecated Set the class in the sheet before read
203
     */
Z
zhuangjiaju 已提交
204
    @Deprecated
Z
zhuangjiaju 已提交
205
    public void read(Sheet sheet, Class clazz) {
clevertension's avatar
clevertension 已提交
206 207 208
        if (sheet != null) {
            sheet.setClazz(clazz);
        }
Z
zhuangjiaju 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
        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();
230 231
    }

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

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

J
update  
jipengfei.jpf 已提交
264
    /**
Z
zhuangjiaju 已提交
265
     * Complete the entire read file.Release the cache and close stream.
J
update  
jipengfei.jpf 已提交
266
     */
Z
zhuangjiaju 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    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 已提交
296 297
    }
}