mirror of https://github.com/alibaba/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.
85 lines
2.7 KiB
85 lines
2.7 KiB
7 years ago
|
package com.alibaba.excel.analysis;
|
||
|
|
||
|
import com.alibaba.excel.context.AnalysisContext;
|
||
|
import com.alibaba.excel.event.AnalysisEventListener;
|
||
|
import com.alibaba.excel.event.AnalysisEventRegisterCenter;
|
||
|
import com.alibaba.excel.event.OneRowAnalysisFinishEvent;
|
||
|
import com.alibaba.excel.metadata.Sheet;
|
||
|
import com.alibaba.excel.support.ExcelTypeEnum;
|
||
|
import com.alibaba.excel.util.TypeUtil;
|
||
7 years ago
|
|
||
|
import java.io.InputStream;
|
||
7 years ago
|
import java.util.ArrayList;
|
||
7 years ago
|
import java.util.LinkedHashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @author jipengfei
|
||
|
*/
|
||
|
public abstract class BaseSaxAnalyser implements AnalysisEventRegisterCenter, ExcelAnalyser {
|
||
|
|
||
|
protected AnalysisContext analysisContext;
|
||
|
|
||
|
private LinkedHashMap<String, AnalysisEventListener> listeners = new LinkedHashMap<String, AnalysisEventListener>();
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
protected abstract void execute();
|
||
|
|
||
|
public void init(InputStream inputStream, ExcelTypeEnum excelTypeEnum, Object custom,
|
||
|
AnalysisEventListener eventListener, boolean trim) {
|
||
|
}
|
||
|
|
||
|
public void appendLister(String name, AnalysisEventListener listener) {
|
||
|
if (!listeners.containsKey(name)) {
|
||
|
listeners.put(name, listener);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void analysis(Sheet sheetParam) {
|
||
|
execute();
|
||
|
}
|
||
|
|
||
|
public void analysis() {
|
||
|
execute();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
public void cleanAllListeners() {
|
||
|
listeners = new LinkedHashMap<String, AnalysisEventListener>();
|
||
|
}
|
||
|
|
||
|
public void notifyListeners(OneRowAnalysisFinishEvent event) {
|
||
|
analysisContext.setCurrentRowAnalysisResult(event.getData());
|
||
|
|
||
|
if (analysisContext.getCurrentRowNum() < analysisContext.getCurrentSheet().getHeadLineMun()) {
|
||
|
if (analysisContext.getCurrentRowNum() <= analysisContext.getCurrentSheet().getHeadLineMun() - 1) {
|
||
|
analysisContext.buildExcelHeadProperty(null,
|
||
|
(List<String>)analysisContext.getCurrentRowAnalysisResult());
|
||
|
}
|
||
|
} else {
|
||
|
analysisContext.setCurrentRowAnalysisResult(event.getData());
|
||
7 years ago
|
if (listeners.size() == 1) {
|
||
|
analysisContext.setCurrentRowAnalysisResult(converter((List<String>)event.getData()));
|
||
|
}
|
||
7 years ago
|
for (Map.Entry<String, AnalysisEventListener> entry : listeners.entrySet()) {
|
||
|
entry.getValue().invoke(analysisContext.getCurrentRowAnalysisResult(), analysisContext);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
|
private List<String> converter(List<String> data) {
|
||
|
List<String> list = new ArrayList<String>();
|
||
|
if (data != null) {
|
||
|
for (String str : data) {
|
||
|
list.add(TypeUtil.formatFloat(str));
|
||
|
}
|
||
|
}
|
||
|
return list;
|
||
|
}
|
||
|
|
||
7 years ago
|
}
|