Jiaju Zhuang
5 years ago
45 changed files with 1070 additions and 870 deletions
@ -1,39 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v03; |
||||
|
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public abstract class AbstractXlsRecordHandler implements XlsRecordHandler { |
||||
protected int row = -1; |
||||
protected int column = -1; |
||||
protected CellData cellData; |
||||
protected XlsReadContext analysisContext; |
||||
|
||||
public AbstractXlsRecordHandler(XlsReadContext analysisContext) { |
||||
this.analysisContext = analysisContext; |
||||
} |
||||
|
||||
@Override |
||||
public int getRow() { |
||||
return row; |
||||
} |
||||
|
||||
@Override |
||||
public int getColumn() { |
||||
return column; |
||||
} |
||||
|
||||
@Override |
||||
public CellData getCellData() { |
||||
return cellData; |
||||
} |
||||
|
||||
@Override |
||||
public int compareTo(XlsRecordHandler o) { |
||||
return this.getOrder() - o.getOrder(); |
||||
} |
||||
} |
@ -0,0 +1,8 @@
|
||||
package com.alibaba.excel.analysis.v03; |
||||
|
||||
/** |
||||
* Need to ignore the current handler without reading the current sheet. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public interface IgnorableXlsRecordHandler extends XlsRecordHandler {} |
@ -1,52 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.BlankRecord; |
||||
import org.apache.poi.hssf.record.BoolErrRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class BlankOrErrorRecordHandler extends AbstractXlsRecordHandler { |
||||
|
||||
public BlankOrErrorRecordHandler(XlsReadContext analysisContext) { |
||||
super(analysisContext); |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Record record) { |
||||
return BlankRecord.sid == record.getSid() || BoolErrRecord.sid == record.getSid(); |
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(Record record) { |
||||
if (record.getSid() == BlankRecord.sid) { |
||||
BlankRecord br = (BlankRecord)record; |
||||
this.row = br.getRow(); |
||||
this.column = br.getColumn(); |
||||
this.cellData = new CellData(CellDataTypeEnum.EMPTY); |
||||
} else if (record.getSid() == BoolErrRecord.sid) { |
||||
BoolErrRecord ber = (BoolErrRecord)record; |
||||
this.row = ber.getRow(); |
||||
this.column = ber.getColumn(); |
||||
this.cellData = new CellData(ber.getBooleanValue()); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void init() { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return 0; |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.BlankRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class BlankRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
BlankRecord br = (BlankRecord)record; |
||||
xlsReadContext.cellMap().put((int)br.getColumn(), CellData.newEmptyInstance(br.getRow(), (int)br.getColumn())); |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.BoolErrRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.enums.RowTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class BoolErrRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
BoolErrRecord ber = (BoolErrRecord)record; |
||||
xlsReadContext.cellMap().put((int)ber.getColumn(), |
||||
CellData.newInstance(ber.getBooleanValue(), ber.getRow(), (int)ber.getColumn())); |
||||
xlsReadContext.tempRowType(RowTypeEnum.DATA); |
||||
} |
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.BoundSheetRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class BoundSheetRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
BoundSheetRecord bsr = (BoundSheetRecord)record; |
||||
xlsReadContext.boundSheetRecordList().add((BoundSheetRecord)record); |
||||
} |
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import java.util.LinkedHashMap; |
||||
|
||||
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; |
||||
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.metadata.Cell; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class DummyRecordRecordHandler implements IgnorableXlsRecordHandler { |
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
if (record instanceof LastCellOfRowDummyRecord) { |
||||
// End of this row
|
||||
LastCellOfRowDummyRecord lcrdr = (LastCellOfRowDummyRecord)record; |
||||
xlsReadContext.rowIndex(lcrdr.getRow()); |
||||
xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadContext.tempRowType(), |
||||
xlsReadContext.readSheetHolder().getGlobalConfiguration())); |
||||
xlsReadContext.analysisEventProcessor().endRow(xlsReadContext); |
||||
xlsReadContext.cellMap(new LinkedHashMap<Integer, Cell>()); |
||||
} else if (record instanceof MissingCellDummyRecord) { |
||||
MissingCellDummyRecord mcdr = (MissingCellDummyRecord)record; |
||||
xlsReadContext.cellMap().put(mcdr.getColumn(), CellData.newEmptyInstance(mcdr.getRow(), mcdr.getColumn())); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class EofRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
if (xlsReadContext.readSheetHolder() != null) { |
||||
xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,38 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.LabelSSTRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.cache.ReadCache; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.enums.RowTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class LabelSSTRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
LabelSSTRecord lsrec = (LabelSSTRecord)record; |
||||
ReadCache readCache = xlsReadContext.readWorkbookHolder().getReadCache(); |
||||
if (readCache == null) { |
||||
xlsReadContext.cellMap().put((int)lsrec.getColumn(), |
||||
CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn())); |
||||
return; |
||||
} |
||||
String data = readCache.get(lsrec.getSSTIndex()); |
||||
if (data == null) { |
||||
xlsReadContext.cellMap().put((int)lsrec.getColumn(), |
||||
CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn())); |
||||
return; |
||||
} |
||||
xlsReadContext.cellMap().put((int)lsrec.getColumn(), |
||||
CellData.newInstance(data, lsrec.getRow(), (int)lsrec.getColumn())); |
||||
xlsReadContext.tempRowType(RowTypeEnum.DATA); |
||||
} |
||||
} |
@ -1,43 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class MissingCellDummyRecordHandler extends AbstractXlsRecordHandler { |
||||
public MissingCellDummyRecordHandler(XlsReadContext analysisContext) { |
||||
super(analysisContext); |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Record record) { |
||||
return record instanceof MissingCellDummyRecord; |
||||
} |
||||
|
||||
@Override |
||||
public void init() { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(Record record) { |
||||
MissingCellDummyRecord mcdr = (MissingCellDummyRecord)record; |
||||
this.row = mcdr.getRow(); |
||||
this.column = mcdr.getColumn(); |
||||
this.cellData = new CellData(CellDataTypeEnum.EMPTY); |
||||
} |
||||
|
||||
@Override |
||||
public int getOrder() { |
||||
return 1; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.CommonObjectDataSubRecord; |
||||
import org.apache.poi.hssf.record.ObjRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
import org.apache.poi.hssf.record.SubRecord; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ObjRecordHandler implements IgnorableXlsRecordHandler { |
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
ObjRecord or = (ObjRecord)record; |
||||
for (SubRecord subRecord : or.getSubRecords()) { |
||||
if (subRecord instanceof CommonObjectDataSubRecord) { |
||||
CommonObjectDataSubRecord codsr = (CommonObjectDataSubRecord)subRecord; |
||||
if (CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT == codsr.getObjectType()) { |
||||
xlsReadContext.tempObjectIndex(codsr.getObjectId()); |
||||
} |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,54 +1,20 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.LabelSSTRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
import org.apache.poi.hssf.record.SSTRecord; |
||||
|
||||
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler; |
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.cache.XlsCache; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class SstRecordHandler extends AbstractXlsRecordHandler { |
||||
private SSTRecord sstRecord; |
||||
|
||||
public SstRecordHandler(XlsReadContext analysisContext) { |
||||
super(analysisContext); |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(Record record) { |
||||
return SSTRecord.sid == record.getSid() || LabelSSTRecord.sid == record.getSid(); |
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(Record record) { |
||||
if (record.getSid() == SSTRecord.sid) { |
||||
sstRecord = (SSTRecord)record; |
||||
} else if (record.getSid() == LabelSSTRecord.sid) { |
||||
LabelSSTRecord lsrec = (LabelSSTRecord)record; |
||||
this.row = lsrec.getRow(); |
||||
this.column = lsrec.getColumn(); |
||||
if (sstRecord == null) { |
||||
this.cellData = new CellData(CellDataTypeEnum.EMPTY); |
||||
} else { |
||||
this.cellData = new CellData(sstRecord.getString(lsrec.getSSTIndex()).toString()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void init() { |
||||
|
||||
} |
||||
|
||||
public class SstRecordHandler implements IgnorableXlsRecordHandler { |
||||
@Override |
||||
public int getOrder() { |
||||
return 0; |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
xlsReadContext.readWorkbookHolder().setReadCache(new XlsCache((SSTRecord)record)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,33 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.Record; |
||||
import org.apache.poi.hssf.record.StringRecord; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.XlsReadContext; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class StringRecordHandler implements IgnorableXlsRecordHandler { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(StringRecordHandler.class); |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
// String for formula
|
||||
StringRecord srec = (StringRecord)record; |
||||
CellData tempCellData = xlsReadContext.tempCellData(); |
||||
if (tempCellData == null) { |
||||
LOGGER.warn("String type formula but no value found."); |
||||
return; |
||||
} |
||||
tempCellData.setStringValue(srec.getString()); |
||||
xlsReadContext.cellMap().put(tempCellData.getColumnIndex(), tempCellData); |
||||
xlsReadContext.tempCellData(null); |
||||
} |
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.alibaba.excel.cache; |
||||
|
||||
import org.apache.poi.hssf.record.SSTRecord; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
|
||||
/** |
||||
* |
||||
* Use SSTRecord. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class XlsCache implements ReadCache { |
||||
private SSTRecord sstRecord; |
||||
|
||||
public XlsCache(SSTRecord sstRecord) { |
||||
this.sstRecord = sstRecord; |
||||
} |
||||
|
||||
@Override |
||||
public void init(AnalysisContext analysisContext) {} |
||||
|
||||
@Override |
||||
public void put(String value) {} |
||||
|
||||
@Override |
||||
public String get(Integer key) { |
||||
return sstRecord.getString(key).toString(); |
||||
} |
||||
|
||||
@Override |
||||
public void putFinished() {} |
||||
|
||||
@Override |
||||
public void destroy() {} |
||||
|
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
/** |
||||
* The types of row |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public enum RowTypeEnum { |
||||
/** |
||||
* data |
||||
*/ |
||||
DATA, |
||||
/** |
||||
* extra |
||||
*/ |
||||
EXTRA, |
||||
/** |
||||
* empty |
||||
*/ |
||||
EMPTY,; |
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
/** |
||||
* cell |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public class AbstractCell implements Cell { |
||||
/** |
||||
* Row index |
||||
*/ |
||||
private Integer rowIndex; |
||||
/** |
||||
* Column index |
||||
*/ |
||||
private Integer columnIndex; |
||||
|
||||
public Integer getRowIndex() { |
||||
return rowIndex; |
||||
} |
||||
|
||||
public void setRowIndex(Integer rowIndex) { |
||||
this.rowIndex = rowIndex; |
||||
} |
||||
|
||||
public Integer getColumnIndex() { |
||||
return columnIndex; |
||||
} |
||||
|
||||
public void setColumnIndex(Integer columnIndex) { |
||||
this.columnIndex = columnIndex; |
||||
} |
||||
} |
@ -0,0 +1,22 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
/** |
||||
* Cell |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public interface Cell { |
||||
/** |
||||
* Row index |
||||
* |
||||
* @return |
||||
*/ |
||||
Integer getRowIndex(); |
||||
|
||||
/** |
||||
* Column index |
||||
* |
||||
* @return |
||||
*/ |
||||
Integer getColumnIndex(); |
||||
} |
@ -1,39 +0,0 @@
|
||||
package com.alibaba.excel.read.listener; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.read.listener.event.AnalysisFinishEvent; |
||||
|
||||
/** |
||||
* Registry center. |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface ReadListenerRegistryCenter { |
||||
|
||||
/** |
||||
* register |
||||
* |
||||
* @param listener |
||||
* Analysis listener |
||||
*/ |
||||
void register(AnalysisEventListener listener); |
||||
|
||||
/** |
||||
* Parse one row to notify all event listeners |
||||
* |
||||
* @param event |
||||
* parse event |
||||
* @param analysisContext |
||||
* Analysis context |
||||
*/ |
||||
void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext); |
||||
|
||||
/** |
||||
* Notify after all analysed |
||||
* |
||||
* @param analysisContext |
||||
* Analysis context |
||||
*/ |
||||
void notifyAfterAllAnalysed(AnalysisContext analysisContext); |
||||
} |
@ -1,20 +0,0 @@
|
||||
package com.alibaba.excel.read.listener.event; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* |
||||
* Event |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface AnalysisFinishEvent { |
||||
/** |
||||
* Get result |
||||
* |
||||
* @return |
||||
*/ |
||||
Map<Integer, CellData> getAnalysisResult(); |
||||
} |
@ -1,21 +0,0 @@
|
||||
package com.alibaba.excel.read.listener.event; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* @author jipengfei |
||||
*/ |
||||
public class EachRowAnalysisFinishEvent implements AnalysisFinishEvent { |
||||
private Map<Integer, CellData> result; |
||||
|
||||
public EachRowAnalysisFinishEvent(Map<Integer, CellData> content) { |
||||
this.result = content; |
||||
} |
||||
|
||||
@Override |
||||
public Map<Integer, CellData> getAnalysisResult() { |
||||
return result; |
||||
} |
||||
} |
@ -0,0 +1,26 @@
|
||||
package com.alibaba.excel.read.processor; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
|
||||
/** |
||||
* |
||||
* Event processor |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface AnalysisEventProcessor { |
||||
/** |
||||
* End row |
||||
* |
||||
* @param analysisContext |
||||
*/ |
||||
void endRow(AnalysisContext analysisContext); |
||||
|
||||
/** |
||||
* Notify after all analysed |
||||
* |
||||
* @param analysisContext |
||||
* Analysis context |
||||
*/ |
||||
void endSheet(AnalysisContext analysisContext); |
||||
} |
@ -0,0 +1,149 @@
|
||||
package com.alibaba.excel.read.processor; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.enums.HeadKindEnum; |
||||
import com.alibaba.excel.exception.ExcelAnalysisException; |
||||
import com.alibaba.excel.exception.ExcelAnalysisStopException; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.read.listener.ReadListener; |
||||
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; |
||||
import com.alibaba.excel.util.ConverterUtils; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Analysis event |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class DefalutAnalysisEventProcessor implements AnalysisEventProcessor { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefalutAnalysisEventProcessor.class); |
||||
|
||||
@Override |
||||
public void endRow(AnalysisContext analysisContext) { |
||||
|
||||
switch (analysisContext.readRowHolder().getRowType()) { |
||||
case EMPTY: |
||||
if (LOGGER.isDebugEnabled()) { |
||||
LOGGER.warn("Empty row!"); |
||||
} |
||||
if (analysisContext.readWorkbookHolder().getIgnoreEmptyRow()) { |
||||
return; |
||||
} |
||||
// Need to continue to notify invoke.
|
||||
dealData(analysisContext); |
||||
break; |
||||
case DATA: |
||||
dealData(analysisContext); |
||||
break; |
||||
case EXTRA: |
||||
dealExtra(analysisContext); |
||||
break; |
||||
default: |
||||
throw new ExcelAnalysisException("Wrong row type."); |
||||
} |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void endSheet(AnalysisContext analysisContext) { |
||||
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { |
||||
readListener.doAfterAllAnalysed(analysisContext); |
||||
} |
||||
} |
||||
|
||||
private void dealExtra(AnalysisContext analysisContext) {} |
||||
|
||||
private void dealData(AnalysisContext analysisContext) { |
||||
ReadRowHolder readRowHolder = analysisContext.readRowHolder(); |
||||
Map<Integer, CellData> cellDataMap = (Map)readRowHolder.getCellMap(); |
||||
readRowHolder.setCurrentRowAnalysisResult(cellDataMap); |
||||
int rowIndex = readRowHolder.getRowIndex(); |
||||
int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber(); |
||||
|
||||
boolean isData = rowIndex >= currentHeadRowNumber; |
||||
|
||||
// Last head column
|
||||
if (!isData && currentHeadRowNumber == rowIndex + 1) { |
||||
buildHead(analysisContext, cellDataMap); |
||||
} |
||||
|
||||
// Now is data
|
||||
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) { |
||||
try { |
||||
if (isData) { |
||||
readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext); |
||||
} else { |
||||
readListener.invokeHead(cellDataMap, analysisContext); |
||||
} |
||||
} catch (Exception e) { |
||||
for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) { |
||||
try { |
||||
readListenerException.onException(e, analysisContext); |
||||
} catch (RuntimeException re) { |
||||
throw re; |
||||
} catch (Exception e1) { |
||||
throw new ExcelAnalysisException(e1.getMessage(), e1); |
||||
} |
||||
} |
||||
break; |
||||
} |
||||
if (!readListener.hasNext(analysisContext)) { |
||||
throw new ExcelAnalysisStopException(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void buildHead(AnalysisContext analysisContext, Map<Integer, CellData> cellDataMap) { |
||||
if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { |
||||
return; |
||||
} |
||||
Map<Integer, String> dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext); |
||||
ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); |
||||
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap(); |
||||
Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); |
||||
Map<Integer, Head> tmpHeadMap = new HashMap<Integer, Head>(headMapData.size() * 4 / 3 + 1); |
||||
Map<Integer, ExcelContentProperty> tmpContentPropertyMap = |
||||
new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1); |
||||
for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) { |
||||
Head headData = entry.getValue(); |
||||
if (headData.getForceIndex() || !headData.getForceName()) { |
||||
tmpHeadMap.put(entry.getKey(), headData); |
||||
tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey())); |
||||
continue; |
||||
} |
||||
List<String> headNameList = headData.getHeadNameList(); |
||||
String headName = headNameList.get(headNameList.size() - 1); |
||||
for (Map.Entry<Integer, String> stringEntry : dataMap.entrySet()) { |
||||
if (stringEntry == null) { |
||||
continue; |
||||
} |
||||
String headString = stringEntry.getValue(); |
||||
Integer stringKey = stringEntry.getKey(); |
||||
if (StringUtils.isEmpty(headString)) { |
||||
continue; |
||||
} |
||||
if (analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { |
||||
headString = headString.trim(); |
||||
} |
||||
if (headName.equals(headString)) { |
||||
headData.setColumnIndex(stringKey); |
||||
tmpHeadMap.put(stringKey, headData); |
||||
tmpContentPropertyMap.put(stringKey, contentPropertyMapData.get(entry.getKey())); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
excelHeadPropertyData.setHeadMap(tmpHeadMap); |
||||
excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap); |
||||
} |
||||
} |
Loading…
Reference in new issue