You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.2 KiB

7 years ago
package com.alibaba.excel;
import java.io.InputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.ExcelAnalyser;
import com.alibaba.excel.analysis.ExcelAnalyserImpl;
import com.alibaba.excel.analysis.ExcelExecutor;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.write.metadata.Sheet;
import com.alibaba.excel.parameter.AnalysisParam;
7 years ago
import com.alibaba.excel.support.ExcelTypeEnum;
/**
* Excel readers are all read in event mode.
7 years ago
*
* @author jipengfei
*/
public class ExcelReader {
private static final Logger LOGGER = LoggerFactory.getLogger(ExcelReader.class);
7 years ago
/**
* Analyser
7 years ago
*/
private ExcelAnalyser excelAnalyser;
private boolean finished = false;
7 years ago
/**
* Create new reader
*
* @param in
* the POI filesystem that contains the Workbook stream
* @param excelTypeEnum
* 03 or 07
* @param customContent
* {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
* @param eventListener
* Callback method after each row is parsed.
7 years ago
*/
@Deprecated
7 years ago
public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
AnalysisEventListener eventListener) {
7 years ago
this(in, excelTypeEnum, customContent, eventListener, true);
}
/**
* Create new reader
*
* @param in
* the POI filesystem that contains the Workbook stream
* @param customContent
* {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
* @param eventListener
* Callback method after each row is parsed
*/
public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener) {
this(in, customContent, eventListener, null, true);
}
7 years ago
/**
* Create new reader
*
* @param in
* the POI filesystem that contains the Workbook stream
* @param customContent
* {@link AnalysisEventListener#invoke(Object, AnalysisContext) }AnalysisContext
* @param eventListener
* Callback method after each row is parsed
*/
public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
List<Converter> converters) {
this(in, customContent, eventListener, converters, true);
}
/**
* Create new reader
*
* @param param
* old param Deprecated
* @param eventListener
* Callback method after each row is parsed.
*/
@Deprecated
public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
}
/**
* Create new reader
*
* @param in
* the POI filesystem that contains the Workbook stream
* @param excelTypeEnum
* 03 or 07
* @param customContent
* {@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.
7 years ago
*/
@Deprecated
7 years ago
public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
AnalysisEventListener eventListener, boolean trim) {
this(in, excelTypeEnum, customContent, eventListener, null, trim);
7 years ago
}
/**
* Create new reader
*
* @param in
* @param customContent
* {@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.
*/
public ExcelReader(InputStream in, Object customContent, AnalysisEventListener eventListener,
List<Converter> converters, boolean trim) {
this(in, ExcelTypeEnum.valueOf(in), customContent, eventListener, converters, trim);
}
public ExcelReader(InputStream in, Object excelTypeEnum, AnalysisEventListener<Object> eventListener,
boolean trim) {
this(in, ExcelTypeEnum.valueOf(in), null, eventListener, null, trim);
}
public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
AnalysisEventListener eventListener, List<Converter> converters, boolean trim) {
validateParam(in, eventListener);
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);
}
}
}
7 years ago
/**
* Parse all sheet content by default
7 years ago
*/
public void read() {
ExcelExecutor excelExecutor = excelAnalyser.excelExecutor();
if (excelExecutor.sheetList().isEmpty()) {
LOGGER.warn("Excel doesn't have any sheets.");
return;
}
for (Sheet sheet : excelExecutor.sheetList()) {
read(sheet);
}
7 years ago
}
/**
* Parse the specified sheetSheetNo start from 1
7 years ago
*
* @param sheet
* Read sheet
7 years ago
*/
public void read(Sheet sheet) {
checkFinished();
excelAnalyser.analysis(sheet);
7 years ago
}
/**
* Parse the specified sheet
*
* @param sheet
* Read sheet
* @param clazz
* object parsed into each row of value
*
* @deprecated Set the class in the sheet before read
*/
@Deprecated
public void read(Sheet sheet, Class clazz) {
if (sheet != null) {
sheet.setClazz(clazz);
}
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();
}
7 years ago
/**
* Parse the workBook get all sheets
7 years ago
*
* @return workBook all sheets
*
* @deprecated please use {@link #excelExecutor()}
7 years ago
*/
@Deprecated
7 years ago
public List<Sheet> getSheets() {
return excelExecutor().sheetList();
7 years ago
}
/**
*
* @return
* @deprecated please use {@link #analysisContext()}
*/
@Deprecated
public AnalysisContext getAnalysisContext() {
return analysisContext();
}
7 years ago
/**
* Complete the entire read file.Release the cache and close stream.
7 years ago
*/
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.");
}
7 years ago
}
}