forked from github/easyexcel
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.
76 lines
2.6 KiB
76 lines
2.6 KiB
6 years ago
|
package com.alibaba.excel.analysis;
|
||
7 years ago
|
|
||
6 years ago
|
import com.alibaba.excel.analysis.v03.XlsSaxAnalyser;
|
||
|
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
|
||
|
import com.alibaba.excel.context.AnalysisContext;
|
||
|
import com.alibaba.excel.context.AnalysisContextImpl;
|
||
|
import com.alibaba.excel.event.AnalysisEventListener;
|
||
|
import com.alibaba.excel.exception.ExcelAnalysisException;
|
||
7 years ago
|
import com.alibaba.excel.metadata.Sheet;
|
||
6 years ago
|
import com.alibaba.excel.modelbuild.ModelBuildEventListener;
|
||
7 years ago
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
||
|
|
||
6 years ago
|
import java.io.InputStream;
|
||
|
import java.util.List;
|
||
|
|
||
7 years ago
|
/**
|
||
|
* @author jipengfei
|
||
|
*/
|
||
|
public class ExcelAnalyserImpl implements ExcelAnalyser {
|
||
|
|
||
|
private AnalysisContext analysisContext;
|
||
|
|
||
|
private BaseSaxAnalyser saxAnalyser;
|
||
|
|
||
|
private BaseSaxAnalyser getSaxAnalyser() {
|
||
|
if (saxAnalyser == null) {
|
||
6 years ago
|
try {
|
||
|
if (ExcelTypeEnum.XLS.equals(analysisContext.getExcelType())) {
|
||
|
this.saxAnalyser = new XlsSaxAnalyser(analysisContext);
|
||
|
} else {
|
||
|
this.saxAnalyser = new XlsxSaxAnalyser(analysisContext);
|
||
7 years ago
|
}
|
||
6 years ago
|
} catch (Exception e) {
|
||
|
throw new ExcelAnalysisException("Analyse excel occur file error fileType " + analysisContext.getExcelType(),
|
||
|
e);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
return this.saxAnalyser;
|
||
|
}
|
||
|
|
||
|
public void init(InputStream inputStream, ExcelTypeEnum excelTypeEnum, Object custom,
|
||
|
AnalysisEventListener eventListener, boolean trim) {
|
||
|
analysisContext = new AnalysisContextImpl(inputStream, excelTypeEnum, custom,
|
||
|
eventListener, trim);
|
||
|
}
|
||
|
|
||
|
public void analysis(Sheet sheetParam) {
|
||
|
analysisContext.setCurrentSheet(sheetParam);
|
||
|
analysis();
|
||
|
}
|
||
|
|
||
|
public void analysis() {
|
||
|
BaseSaxAnalyser saxAnalyser = getSaxAnalyser();
|
||
|
appendListeners(saxAnalyser);
|
||
|
saxAnalyser.execute();
|
||
|
|
||
|
analysisContext.getEventListener().doAfterAllAnalysed(analysisContext);
|
||
|
}
|
||
|
|
||
|
public List<Sheet> getSheets() {
|
||
|
BaseSaxAnalyser saxAnalyser = getSaxAnalyser();
|
||
|
saxAnalyser.cleanAllListeners();
|
||
|
return saxAnalyser.getSheets();
|
||
|
}
|
||
|
|
||
|
private void appendListeners(BaseSaxAnalyser saxAnalyser) {
|
||
|
if (analysisContext.getCurrentSheet() != null && analysisContext.getCurrentSheet().getClazz() != null) {
|
||
|
saxAnalyser.appendLister("model_build_listener", new ModelBuildEventListener());
|
||
|
}
|
||
|
if (analysisContext.getEventListener() != null) {
|
||
|
saxAnalyser.appendLister("user_define_listener", analysisContext.getEventListener());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|