Browse Source

新增支持接收批注

pull/1117/head
Jiaju Zhuang 5 years ago
parent
commit
f0d9051db2
  1. 39
      src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java
  2. 8
      src/main/java/com/alibaba/excel/analysis/v03/IgnorableXlsRecordHandler.java
  3. 4
      src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java
  4. 49
      src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java
  5. 141
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  6. 52
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java
  7. 22
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java
  8. 112
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
  9. 25
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java
  10. 21
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java
  11. 36
      src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java
  12. 21
      src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
  13. 95
      src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java
  14. 28
      src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java
  15. 33
      src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java
  16. 38
      src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java
  17. 43
      src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java
  18. 40
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java
  19. 47
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  20. 30
      src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java
  21. 32
      src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java
  22. 44
      src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java
  23. 33
      src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java
  24. 35
      src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java
  25. 4
      src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java
  26. 37
      src/main/java/com/alibaba/excel/cache/XlsCache.java
  27. 37
      src/main/java/com/alibaba/excel/context/AnalysisContext.java
  28. 12
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  29. 110
      src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java
  30. 84
      src/main/java/com/alibaba/excel/context/XlsReadContext.java
  31. 21
      src/main/java/com/alibaba/excel/enums/RowTypeEnum.java
  32. 33
      src/main/java/com/alibaba/excel/metadata/AbstractCell.java
  33. 22
      src/main/java/com/alibaba/excel/metadata/Cell.java
  34. 38
      src/main/java/com/alibaba/excel/metadata/CellData.java
  35. 12
      src/main/java/com/alibaba/excel/metadata/CellExtra.java
  36. 39
      src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java
  37. 20
      src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java
  38. 21
      src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java
  39. 133
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  40. 32
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java
  41. 15
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  42. 26
      src/main/java/com/alibaba/excel/read/processor/AnalysisEventProcessor.java
  43. 149
      src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java
  44. 18
      src/main/java/com/alibaba/excel/util/SheetUtils.java
  45. 1
      update.md

39
src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java

@ -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();
}
}

8
src/main/java/com/alibaba/excel/analysis/v03/IgnorableXlsRecordHandler.java

@ -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 {}

4
src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java

@ -25,9 +25,10 @@ import com.alibaba.excel.read.metadata.ReadSheet;
*/
public class XlsListSheetListener implements HSSFListener {
private POIFSFileSystem poifsFileSystem;
private List<ReadSheet> sheetList;
private BofRecordHandler bofRecordHandler;
private static fin
public XlsListSheetListener(XlsReadContext analysisContext, POIFSFileSystem poifsFileSystem) {
this.poifsFileSystem = poifsFileSystem;
sheetList = new ArrayList<ReadSheet>();
@ -49,7 +50,6 @@ public class XlsListSheetListener implements HSSFListener {
EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener =
new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
request.addListenerForAllRecords(workbookBuildingListener);
try {
factory.processWorkbookEvents(request, poifsFileSystem);
} catch (IOException e) {

49
src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java

@ -2,60 +2,19 @@ package com.alibaba.excel.analysis.v03;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.context.XlsReadContext;
/**
* Intercepts handle xls reads.
*
* @author Dan Zheng
*/
public interface XlsRecordHandler extends Comparable<XlsRecordHandler> {
/**
* Which tags are supported
*
* @param record
* Excel analysis record
* @return Which tags are supported
*/
boolean support(Record record);
/**
* Initialize
*/
void init();
public interface XlsRecordHandler {
/**
* Processing record
*
* @param xlsReadContext
* @param record
*/
void processRecord(Record record);
/**
* Get row
*
* @return Row index
*/
int getRow();
/**
* Get column
*
* @return Column index
*/
int getColumn();
/**
* Get value
*
* @return Excel internal cell data
*/
CellData getCellData();
/**
* Get order
*
* @return Order
*/
int getOrder();
void processRecord(XlsReadContext xlsReadContext, Record record);
}

141
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -3,7 +3,7 @@ package com.alibaba.excel.analysis.v03;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -15,15 +15,16 @@ import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.ExcelReadExecutor;
import com.alibaba.excel.analysis.v03.handlers.BlankOrErrorRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BlankRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.FormulaRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.IndexRecordHandler;
@ -60,44 +61,56 @@ import com.alibaba.excel.util.CollectionUtils;
*/
public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
//
// private Boolean readAll;
// private List<ReadSheet> readSheetList;
// /**
// * For parsing Formulas
// */
// private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener;
// private FormatTrackingHSSFListener formatListener;
// private List<ReadSheet> sheets;
// private HSSFWorkbook stubWorkbook;
// private Map<Short, XlsRecordHandler> recordHandlerMap;
private XlsReadContext xlsReadContext;
private POIFSFileSystem poifsFileSystem;
private Boolean readAll;
private List<ReadSheet> readSheetList;
private int lastRowNumber;
private int lastColumnNumber;
private int ii = 0;
/**
* For parsing Formulas
*/
private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener;
private FormatTrackingHSSFListener formatListener;
private Map<Integer, CellData> records;
private List<ReadSheet> sheets;
private HSSFWorkbook stubWorkbook;
private List<XlsRecordHandler> recordHandlers = new ArrayList<XlsRecordHandler>();
private XlsReadContext analysisContext;
public XlsSaxAnalyser(XlsReadContext context, POIFSFileSystem poifsFileSystem) {
this.analysisContext = context;
this.records = new LinkedHashMap<Integer, CellData>();
private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32);
static {
XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler());
}
public XlsSaxAnalyser(XlsReadContext xlsReadContext, POIFSFileSystem poifsFileSystem) {
this.xlsReadContext = xlsReadContext;
this.poifsFileSystem = poifsFileSystem;
analysisContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
xlsReadContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
}
@Override
public List<ReadSheet> sheetList() {
if (sheets == null) {
if (xlsReadContext.readSheetDataList() == null) {
earlySheetDataList();
LOGGER.warn("Getting the 'sheetList' before reading will cause the file to be read twice.");
XlsListSheetListener xlsListSheetListener = new XlsListSheetListener(analysisContext, poifsFileSystem);
sheets = xlsListSheetListener.getSheetList();
XlsListSheetListener xlsListSheetListener = new XlsListSheetListener(xlsReadContext, poifsFileSystem);
}
return xlsReadContext.readSheetDataList();
}
return sheets;
private void earlySheetDataList() {
LOGGER.warn("Getting the 'sheetList' before reading will cause the file to be read twice.");
sheetList = new ArrayList<ReadSheet>();
bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false, false);
bofRecordHandler.init();
bofRecordHandler.init(null, true);
XlsListSheetListener xlsListSheetListener = new XlsListSheetListener(xlsReadContext, poifsFileSystem);
}
@Override
public void execute(List<ReadSheet> readSheetList, Boolean readAll) {
public void execute() {
this.readAll = readAll;
this.readSheetList = readSheetList;
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
@ -116,24 +129,27 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
throw new ExcelAnalysisException(e);
}
// Sometimes tables lack the end record of the last column
if (!records.isEmpty()) {
if (!xlsReadContext.cellMap().isEmpty()) {
endRow();
}
}
private void init() {
lastRowNumber = 0;
lastColumnNumber = 0;
records = new LinkedHashMap<Integer, CellData>();
recordHandlerMap = new HashMap<Short, XlsRecordHandler>(32);
buildXlsRecordHandlers();
}
@Override
public void processRecord(Record record) {
// Not data from the current sheet
if (ignoreRecord(record)) {
XlsRecordHandler handler = recordHandlerMap.get(record.getSid());
if ((handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.readWorkbookHolder().getIgnoreRecord03()) {
// No need to read the current sheet
return;
}
handler.processRecord(xlsReadContext, record);
int thisRow = -1;
int thisColumn = -1;
CellData cellData = null;
@ -153,69 +169,52 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
}
}
// If we got something to print out, do so
if (cellData != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()
if (cellData != null && xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()
&& CellDataTypeEnum.STRING == cellData.getType()) {
cellData.setStringValue(cellData.getStringValue().trim());
}
// Handle new row
if (thisRow != -1 && thisRow != lastRowNumber) {
lastColumnNumber = -1;
}
// Update column and row count
if (thisRow > -1) {
lastRowNumber = thisRow;
}
if (thisColumn > -1) {
lastColumnNumber = thisColumn;
}
processLastCellOfRow(record);
}
private boolean ignoreRecord(Record record) {
return analysisContext.readWorkbookHolder().getIgnoreRecord03() && record.getSid() != BoundSheetRecord.sid
return xlsReadContext.readWorkbookHolder().getIgnoreRecord03() && record.getSid() != BoundSheetRecord.sid
&& record.getSid() != BOFRecord.sid;
}
private void processLastCellOfRow(Record record) {
// Handle end of row
if (record instanceof LastCellOfRowDummyRecord) {
System.out.println("----" + record.getSid());
endRow();
}
}
private void endRow() {
if (lastColumnNumber == -1) {
lastColumnNumber = 0;
}
analysisContext.readRowHolder(
new ReadRowHolder(lastRowNumber, analysisContext.readSheetHolder().getGlobalConfiguration()));
analysisContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), analysisContext);
this.records = new LinkedHashMap<Integer, CellData>();
lastColumnNumber = -1;
xlsReadContext
.readRowHolder(new ReadRowHolder(lastRowNumber, xlsReadContext.readSheetHolder().getGlobalConfiguration()));
xlsReadContext.readSheetHolder().notifyEndOneRow(new EachRowAnalysisFinishEvent(records), xlsReadContext);
}
private void buildXlsRecordHandlers() {
if (CollectionUtils.isEmpty(recordHandlers)) {
recordHandlers.add(new BlankOrErrorRecordHandler(analysisContext));
recordHandlers.add(new BlankOrErrorRecordHandler(xlsReadContext));
// The table has been counted and there are no duplicate statistics
if (sheets == null) {
sheets = new ArrayList<ReadSheet>();
recordHandlers.add(new BofRecordHandler(analysisContext, sheets, false, true));
recordHandlers.add(new BofRecordHandler(xlsReadContext, sheets, false, true));
} else {
recordHandlers.add(new BofRecordHandler(analysisContext, sheets, true, true));
}
recordHandlers.add(new FormulaRecordHandler(analysisContext, stubWorkbook, formatListener));
recordHandlers.add(new LabelRecordHandler(analysisContext));
recordHandlers.add(new NoteRecordHandler(analysisContext));
recordHandlers.add(new NumberRecordHandler(analysisContext, formatListener));
recordHandlers.add(new RkRecordHandler(analysisContext));
recordHandlers.add(new SstRecordHandler(analysisContext));
recordHandlers.add(new MissingCellDummyRecordHandler(analysisContext));
recordHandlers.add(new IndexRecordHandler(analysisContext));
recordHandlers.add(new TextObjectRecordHandler(analysisContext));
recordHandlers.add(new BofRecordHandler(xlsReadContext, sheets, true, true));
}
recordHandlers.add(new FormulaRecordHandler(xlsReadContext, stubWorkbook, formatListener));
recordHandlers.add(new LabelRecordHandler(xlsReadContext));
recordHandlers.add(new NoteRecordHandler(xlsReadContext));
recordHandlers.add(new NumberRecordHandler(xlsReadContext, formatListener));
recordHandlers.add(new RkRecordHandler(xlsReadContext));
recordHandlers.add(new SstRecordHandler(xlsReadContext));
recordHandlers.add(new MissingCellDummyRecordHandler(xlsReadContext));
recordHandlers.add(new IndexRecordHandler(xlsReadContext));
recordHandlers.add(new TextObjectRecordHandler(xlsReadContext));
Collections.sort(recordHandlers);
}

52
src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java

@ -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;
}
}

22
src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java

@ -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()));
}
}

112
src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java

@ -7,8 +7,9 @@ import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.XlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.SheetUtils;
@ -17,91 +18,52 @@ import com.alibaba.excel.util.SheetUtils;
*
* @author Dan Zheng
*/
public class BofRecordHandler extends AbstractXlsRecordHandler {
private List<BoundSheetRecord> boundSheetRecords = new ArrayList<BoundSheetRecord>();
private BoundSheetRecord[] orderedBsrs;
private int sheetIndex;
private List<ReadSheet> sheets;
private Boolean readAll;
private List<ReadSheet> readSheetList;
private boolean alreadyInit;
private boolean needInitSheet;
public BofRecordHandler(XlsReadContext analysisContext, List<ReadSheet> sheets, boolean alreadyInit,
boolean needInitSheet) {
super(analysisContext);
this.sheets = sheets;
this.alreadyInit = alreadyInit;
this.needInitSheet = needInitSheet;
}
@Override
public boolean support(Record record) {
return BoundSheetRecord.sid == record.getSid() || BOFRecord.sid == record.getSid();
}
public class BofRecordHandler implements XlsRecordHandler {
@Override
public void processRecord(Record record) {
if (record.getSid() == BoundSheetRecord.sid) {
boundSheetRecords.add((BoundSheetRecord)record);
} else if (record.getSid() == BOFRecord.sid) {
public void processRecord(XlsReadContext xlsReadContext, Record record) {
BOFRecord br = (BOFRecord)record;
if (br.getType() == BOFRecord.TYPE_WORKSHEET) {
if (orderedBsrs == null) {
orderedBsrs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
String sheetName = orderedBsrs[sheetIndex].getSheetname();
// Find the currently read sheet
ReadSheet readSheet = null;
if (!alreadyInit) {
readSheet = new ReadSheet(sheetIndex, sheetName);
sheets.add(readSheet);
}
if (needInitSheet) {
if (readSheet == null) {
for (ReadSheet sheet : sheets) {
if (sheet.getSheetNo() == sheetIndex) {
readSheet = sheet;
break;
}
if (br.getType() != BOFRecord.TYPE_WORKSHEET) {
return;
}
// Init read sheet Data
initReadSheetDataList(xlsReadContext);
Integer readSheetIndex = xlsReadContext.readSheetIndex();
if (readSheetIndex == null) {
readSheetIndex = 0;
xlsReadContext.readSheetIndex(readSheetIndex);
}
ReadSheet readSheet = xlsReadContext.readSheetDataList().get(readSheetIndex);
assert readSheet != null : "Can't find the sheet.";
analysisContext.readWorkbookHolder().setIgnoreRecord03(Boolean.TRUE);
// Copy the parameter to the current sheet
readSheet = SheetUtils.match(readSheet, readSheetList, readAll,
analysisContext.readWorkbookHolder().getGlobalConfiguration());
readSheet = SheetUtils.match(readSheet, xlsReadContext);
if (readSheet != null) {
if (readSheet.getSheetNo() != 0 && analysisContext.readSheetHolder() != null) {
// Prompt for the end of the previous form read
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext);
}
analysisContext.currentSheet(readSheet);
analysisContext.readWorkbookHolder().setIgnoreRecord03(Boolean.FALSE);
}
}
sheetIndex++;
}
xlsReadContext.currentSheet0(readSheet);
xlsReadContext.ignoreRecord(Boolean.FALSE);
} else {
xlsReadContext.ignoreRecord(Boolean.TRUE);
}
// Go read the next one
xlsReadContext.readSheetIndex(xlsReadContext.readSheetIndex() + 1);
}
@Override
public void init() {
sheetIndex = 0;
orderedBsrs = null;
boundSheetRecords.clear();
if (!alreadyInit) {
sheets.clear();
}
private void initReadSheetDataList(XlsReadContext xlsReadContext) {
if (xlsReadContext.readSheetDataList() != null) {
return;
}
BoundSheetRecord[] boundSheetRecords =
BoundSheetRecord.orderByBofPosition(xlsReadContext.boundSheetRecordList());
List<ReadSheet> readSheetDataList = new ArrayList<ReadSheet>();
for (int i = 0; i < boundSheetRecords.length; i++) {
BoundSheetRecord boundSheetRecord = boundSheetRecords[i];
ReadSheet readSheet = new ReadSheet(i, boundSheetRecord.getSheetname());
readSheetDataList.add(readSheet);
}
xlsReadContext.readSheetDataList(readSheetDataList);
// Just need to get the list of sheets
if (!xlsReadContext.needReadSheet()) {
throw new ExcelAnalysisStopException("Just need to get the list of sheets.");
}
public void init(List<ReadSheet> readSheetList, Boolean readAll) {
this.readSheetList = readSheetList;
this.readAll = readAll;
}
@Override
public int getOrder() {
return 0;
}
}

25
src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java

@ -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);
}
}

21
src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java

@ -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);
}
}

36
src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java

@ -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()));
}
}
}

21
src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java

@ -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);
}
}
}

95
src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java

@ -2,17 +2,14 @@ package com.alibaba.excel.analysis.v03.handlers;
import java.math.BigDecimal;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
@ -22,97 +19,51 @@ import com.alibaba.excel.metadata.CellData;
*
* @author Dan Zheng
*/
public class FormulaRecordHandler extends AbstractXlsRecordHandler {
public class FormulaRecordHandler implements IgnorableXlsRecordHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(FormulaRecordHandler.class);
private static final String ERROR = "#VALUE!";
private int nextRow;
private int nextColumn;
private boolean outputNextStringRecord;
private CellData tempCellData;
private FormatTrackingHSSFListener formatListener;
private HSSFWorkbook stubWorkbook;
public FormulaRecordHandler(XlsReadContext analysisContext, HSSFWorkbook stubWorkbook,
FormatTrackingHSSFListener formatListener) {
super(analysisContext);
this.stubWorkbook = stubWorkbook;
this.formatListener = formatListener;
}
@Override
public boolean support(Record record) {
return FormulaRecord.sid == record.getSid() || StringRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
if (record.getSid() == FormulaRecord.sid) {
public void processRecord(XlsReadContext xlsReadContext, Record record) {
FormulaRecord frec = (FormulaRecord)record;
this.row = frec.getRow();
this.column = frec.getColumn();
CellData tempCellData = new CellData();
tempCellData.setRowIndex(frec.getRow());
tempCellData.setColumnIndex((int)frec.getColumn());
CellType cellType = CellType.forInt(frec.getCachedResultType());
String formulaValue = null;
try {
formulaValue = HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression());
formulaValue = HSSFFormulaParser.toFormulaString(xlsReadContext.hsffWorkbook(), frec.getParsedExpression());
} catch (Exception e) {
LOGGER.warn("Get formula value error.{}", e.getMessage());
LOGGER.debug("Get formula value error.", e);
}
tempCellData.setFormula(Boolean.TRUE);
tempCellData.setFormulaValue(formulaValue);
switch (cellType) {
case STRING:
// Formula result is a string
// This is stored in the next record
outputNextStringRecord = true;
nextRow = frec.getRow();
nextColumn = frec.getColumn();
tempCellData = new CellData(CellDataTypeEnum.STRING);
tempCellData.setFormula(Boolean.TRUE);
tempCellData.setFormulaValue(formulaValue);
tempCellData.setType(CellDataTypeEnum.STRING);
xlsReadContext.tempCellData(tempCellData);
break;
case NUMERIC:
this.cellData = new CellData(BigDecimal.valueOf(frec.getValue()));
this.cellData.setFormula(Boolean.TRUE);
this.cellData.setFormulaValue(formulaValue);
tempCellData.setType(CellDataTypeEnum.NUMBER);
tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue()));
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData);
break;
case ERROR:
this.cellData = new CellData(CellDataTypeEnum.ERROR);
this.cellData.setStringValue(ERROR);
this.cellData.setFormula(Boolean.TRUE);
this.cellData.setFormulaValue(formulaValue);
tempCellData.setType(CellDataTypeEnum.ERROR);
tempCellData.setStringValue(ERROR);
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData);
break;
case BOOLEAN:
this.cellData = new CellData(frec.getCachedBooleanValue());
this.cellData.setFormula(Boolean.TRUE);
this.cellData.setFormulaValue(formulaValue);
tempCellData.setType(CellDataTypeEnum.BOOLEAN);
tempCellData.setBooleanValue(frec.getCachedBooleanValue());
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData);
break;
default:
this.cellData = new CellData(CellDataTypeEnum.EMPTY);
this.cellData.setFormula(Boolean.TRUE);
this.cellData.setFormulaValue(formulaValue);
tempCellData.setType(CellDataTypeEnum.EMPTY);
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData);
break;
}
} else if (record.getSid() == StringRecord.sid) {
if (outputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord)record;
this.cellData = tempCellData;
this.cellData.setStringValue(srec.getString());
this.row = nextRow;
this.column = nextColumn;
outputNextStringRecord = false;
tempCellData = null;
}
}
}
@Override
public void init() {
this.nextRow = 0;
this.nextColumn = 0;
}
@Override
public int getOrder() {
return 0;
}
}

28
src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java

@ -3,7 +3,7 @@ package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
/**
@ -11,30 +11,12 @@ import com.alibaba.excel.context.XlsReadContext;
*
* @author Jiaju Zhuang
*/
public class IndexRecordHandler extends AbstractXlsRecordHandler {
public IndexRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return record instanceof IndexRecord;
}
public class IndexRecordHandler implements IgnorableXlsRecordHandler {
@Override
public void init() {}
@Override
public void processRecord(Record record) {
if (analysisContext.readSheetHolder() == null) {
public void processRecord(XlsReadContext xlsReadContext, Record record) {
if (xlsReadContext.readSheetHolder() == null) {
return;
}
analysisContext.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1());
}
@Override
public int getOrder() {
return 1;
xlsReadContext.readSheetHolder().setApproximateTotalRowNumber(((IndexRecord)record).getLastRowAdd1());
}
}

33
src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java

@ -3,8 +3,9 @@ package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
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;
/**
@ -12,32 +13,12 @@ import com.alibaba.excel.metadata.CellData;
*
* @author Dan Zheng
*/
public class LabelRecordHandler extends AbstractXlsRecordHandler {
public LabelRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
public class LabelRecordHandler implements IgnorableXlsRecordHandler {
@Override
public boolean support(Record record) {
return LabelRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
public void processRecord(XlsReadContext xlsReadContext, Record record) {
LabelRecord lrec = (LabelRecord)record;
this.row = lrec.getRow();
this.column = lrec.getColumn();
this.cellData = new CellData(lrec.getValue());
}
@Override
public void init() {
}
@Override
public int getOrder() {
return 0;
xlsReadContext.cellMap().put((int)lrec.getColumn(),
CellData.newInstance(lrec.getValue(), lrec.getRow(), (int)lrec.getColumn()));
xlsReadContext.tempRowType(RowTypeEnum.DATA);
}
}

38
src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java

@ -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);
}
}

43
src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java

@ -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;
}
}

40
src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java

@ -3,41 +3,27 @@ package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellExtra;
/**
* Record handler
*
* @author Dan Zheng
*/
public class NoteRecordHandler extends AbstractXlsRecordHandler {
public NoteRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return NoteRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
NoteRecord nrec = (NoteRecord)record;
this.row = nrec.getRow();
this.column = nrec.getColumn();
this.cellData = new CellData(CellDataTypeEnum.EMPTY);
}
@Override
public void init() {
}
public class NoteRecordHandler implements IgnorableXlsRecordHandler {
@Override
public int getOrder() {
return 0;
public void processRecord(XlsReadContext xlsReadContext, Record record) {
NoteRecord nr = (NoteRecord)record;
String note = xlsReadContext.objectCacheMap().get(nr.getShapeId());
CellExtra cellExtra = new CellExtra();
cellExtra.setRowIndex(nr.getRow());
cellExtra.setRowIndex(nr.getColumn());
cellExtra.setNote(note);
xlsReadContext.cellMap().put(nr.getColumn(), cellExtra);
xlsReadContext.tempRowType(RowTypeEnum.EXTRA);
}
}

47
src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java

@ -2,13 +2,13 @@ package com.alibaba.excel.analysis.v03.handlers;
import java.math.BigDecimal;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellData;
/**
@ -16,39 +16,18 @@ import com.alibaba.excel.metadata.CellData;
*
* @author Dan Zheng
*/
public class NumberRecordHandler extends AbstractXlsRecordHandler {
private FormatTrackingHSSFListener formatListener;
public NumberRecordHandler(XlsReadContext analysisContext, FormatTrackingHSSFListener formatListener) {
super(analysisContext);
this.formatListener = formatListener;
}
@Override
public boolean support(Record record) {
return NumberRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
NumberRecord numrec = (NumberRecord)record;
this.row = numrec.getRow();
this.column = numrec.getColumn();
this.cellData = new CellData(BigDecimal.valueOf(numrec.getValue()));
int dataFormat = formatListener.getFormatIndex(numrec);
this.cellData.setDataFormat(dataFormat);
this.cellData
.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, formatListener.getFormatString(numrec),
analysisContext.readSheetHolder().getGlobalConfiguration().getLocale()));
}
@Override
public void init() {
}
public class NumberRecordHandler implements IgnorableXlsRecordHandler {
@Override
public int getOrder() {
return 0;
public void processRecord(XlsReadContext xlsReadContext, Record record) {
NumberRecord nr = (NumberRecord)record;
CellData cellData = CellData.newInstance(BigDecimal.valueOf(nr.getValue()), nr.getRow(), (int)nr.getColumn());
Integer dataFormat = xlsReadContext.formatTrackingHSSFListener().getFormatIndex(nr);
cellData.setDataFormat(dataFormat);
cellData.setDataFormatString(
BuiltinFormats.getBuiltinFormat(dataFormat, xlsReadContext.formatTrackingHSSFListener().getFormatString(nr),
xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale()));
xlsReadContext.cellMap().put((int)nr.getColumn(), cellData);
xlsReadContext.tempRowType(RowTypeEnum.DATA);
}
}

30
src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java

@ -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;
}
}
}
}

32
src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java

@ -3,9 +3,8 @@ package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
/**
@ -13,32 +12,11 @@ import com.alibaba.excel.metadata.CellData;
*
* @author Dan Zheng
*/
public class RkRecordHandler extends AbstractXlsRecordHandler {
public RkRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return RKRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
RKRecord rkrec = (RKRecord)record;
this.row = rkrec.getRow();
this.row = rkrec.getColumn();
this.cellData = new CellData(CellDataTypeEnum.EMPTY);
}
@Override
public void init() {
}
public class RkRecordHandler implements IgnorableXlsRecordHandler {
@Override
public int getOrder() {
return 0;
public void processRecord(XlsReadContext xlsReadContext, Record record) {
RKRecord re = (RKRecord)record;
xlsReadContext.cellMap().put((int)re.getColumn(), CellData.newEmptyInstance(re.getRow(), (int)re.getColumn()));
}
}

44
src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java

@ -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));
}
}

33
src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java

@ -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);
}
}

35
src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java

@ -1,10 +1,11 @@
package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.TextObjectRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.AbstractXlsRecordHandler;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext;
/**
@ -12,28 +13,18 @@ import com.alibaba.excel.context.XlsReadContext;
*
* @author Jiaju Zhuang
*/
public class TextObjectRecordHandler extends AbstractXlsRecordHandler {
public TextObjectRecordHandler(XlsReadContext analysisContext) {
super(analysisContext);
}
@Override
public boolean support(Record record) {
return TextObjectRecord.sid == record.getSid() || ObjRecord.sid == record.getSid();
}
@Override
public void processRecord(Record record) {
System.out.println(record);
}
public class TextObjectRecordHandler implements IgnorableXlsRecordHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(TextObjectRecordHandler.class);
@Override
public void init() {
public void processRecord(XlsReadContext xlsReadContext, Record record) {
TextObjectRecord tor = (TextObjectRecord)record;
Integer tempObjectIndex = xlsReadContext.tempObjectIndex();
if (tempObjectIndex == null) {
LOGGER.debug("tempObjectIndex is null.");
return;
}
@Override
public int getOrder() {
return 0;
xlsReadContext.objectCacheMap().put(tempObjectIndex, tor.getStr().getString());
xlsReadContext.tempObjectIndex(null);
}
}

4
src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java

@ -2,8 +2,6 @@ package com.alibaba.excel.analysis.v07;
import java.util.List;
import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.util.StringUtils;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
@ -12,7 +10,9 @@ import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.alibaba.excel.constant.ExcelXmlConstants;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.util.StringUtils;
/**
*

37
src/main/java/com/alibaba/excel/cache/XlsCache.java vendored

@ -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() {}
}

37
src/main/java/com/alibaba/excel/context/AnalysisContext.java

@ -1,6 +1,7 @@
package com.alibaba.excel.context;
import java.io.InputStream;
import java.util.List;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
@ -9,6 +10,7 @@ import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.read.processor.AnalysisEventProcessor;
import com.alibaba.excel.support.ExcelTypeEnum;
/**
@ -69,6 +71,41 @@ public interface AnalysisContext {
*/
Object getCustom();
/**
* Event processor
*
* @return
*/
AnalysisEventProcessor analysisEventProcessor();
/**
* Data that the customer needs to read
*
* @return
*/
List<ReadSheet> readSheetList();
/**
* Data that the customer needs to read
*
* @return
*/
void readSheetList(List<ReadSheet> readSheetList);
/**
* Read all sheets
*
* @return
*/
Boolean readAll();
/**
* Read all sheets
*
* @return
*/
void readAll(Boolean readAll);
/**
* get current sheet
*

12
src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java

@ -13,6 +13,8 @@ import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.read.processor.AnalysisEventProcessor;
import com.alibaba.excel.read.processor.DefalutAnalysisEventProcessor;
import com.alibaba.excel.support.ExcelTypeEnum;
/**
@ -37,6 +39,10 @@ public class AnalysisContextImpl implements AnalysisContext {
* Configuration of currently operated cell
*/
private ReadHolder currentReadHolder;
/**
* Event processor
*/
private AnalysisEventProcessor analysisEventProcessor;
public AnalysisContextImpl(ReadWorkbook readWorkbook) {
if (readWorkbook == null) {
@ -44,6 +50,7 @@ public class AnalysisContextImpl implements AnalysisContext {
}
readWorkbookHolder = new ReadWorkbookHolder(readWorkbook);
currentReadHolder = readWorkbookHolder;
analysisEventProcessor = new DefalutAnalysisEventProcessor();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'AnalysisContextImpl' complete");
}
@ -92,6 +99,11 @@ public class AnalysisContextImpl implements AnalysisContext {
return readWorkbookHolder.getCustomObject();
}
@Override
public AnalysisEventProcessor analysisEventProcessor() {
return analysisEventProcessor;
}
@Override
public Sheet getCurrentSheet() {
Sheet sheet = new Sheet(readSheetHolder.getSheetNo() + 1);

110
src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java

@ -1,5 +1,17 @@
package com.alibaba.excel.context;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.record.BoundSheetRecord;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook;
/**
@ -9,8 +21,106 @@ import com.alibaba.excel.read.metadata.ReadWorkbook;
* @author Jiaju Zhuang
*/
public class DefaultXlsReadContext extends AnalysisContextImpl implements XlsReadContext {
private Map<Integer, Cell> cellMap;
private RowTypeEnum rowType;
private Integer rowIndex;
private CellData cellData;
private CellExtra cellExtra;
/**
* Excel 2003 cannot read specific sheet. It can only read sheet by sheet.So when you specify one sheet, you ignore
* the others.
*/
private Boolean ignoreRecord;
/**
* Bound sheet record
*
* @return
*/
List<BoundSheetRecord> boundSheetRecordList;
public DefaultXlsReadContext(ReadWorkbook readWorkbook) {
super(readWorkbook);
cellMap = new LinkedHashMap<Integer, Cell>();
rowType = RowTypeEnum.EMPTY;
boundSheetRecordList = new ArrayList<BoundSheetRecord>();
}
@Override
public RowTypeEnum rowType() {
return null;
}
@Override
public void rowType(RowTypeEnum rowType) {
}
@Override
public RowTypeEnum tempRowType() {
return null;
}
@Override
public void tempRowType(RowTypeEnum tempRowType) {
}
@Override
public CellData cellData() {
return null;
}
@Override
public void cellData(CellData cellData) {
}
@Override
public CellExtra cellExtra() {
return null;
}
@Override
public void cellExtra(CellExtra cellExtra) {
}
@Override
public Map<Integer, Cell> cellMap() {
return null;
}
@Override
public void cellMap(Map<Integer, Cell> cellMap) {
}
@Override
public Integer rowIndex() {
return null;
}
@Override
public void rowIndex(Integer rowIndex) {
}
@Override
public Boolean ignoreRecord() {
return null;
}
@Override
public void ignoreRecord(Boolean ignoreRecord) {
}
@Override
public void currentSheet0(ReadSheet readSheet) {
currentSheet(readSheet);
cellMap = new LinkedHashMap<Integer, Cell>();
ignoreRecord = Boolean.FALSE;
rowType = RowTypeEnum.EMPTY;
}
}

84
src/main/java/com/alibaba/excel/context/XlsReadContext.java

@ -1,5 +1,18 @@
package com.alibaba.excel.context;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.metadata.ReadSheet;
/**
*
* A context is the main anchorage point of a ls xls reader.
@ -8,4 +21,75 @@ package com.alibaba.excel.context;
*/
public interface XlsReadContext extends AnalysisContext {
RowTypeEnum tempRowType();
void tempRowType(RowTypeEnum tempRowType);
CellData cellData();
void cellData(CellData cellData);
CellExtra cellExtra();
void cellExtra(CellExtra cellExtra);
Map<Integer, Cell> cellMap();
void cellMap(Map<Integer, Cell> cellMap);
Integer rowIndex();
void rowIndex(Integer rowIndex);
Boolean ignoreRecord();
void ignoreRecord(Boolean ignoreRecord);
/**
* Select the current table
*
* @param readSheet
* sheet to read
*/
void currentSheet0(ReadSheet readSheet);
FormatTrackingHSSFListener formatTrackingHSSFListener();
HSSFWorkbook hsffWorkbook();
List<BoundSheetRecord> boundSheetRecordList();
void boundSheetRecordList(List<BoundSheetRecord> boundSheetRecordList);
/**
* Actual data
*
* @return
*/
List<ReadSheet> readSheetDataList();
/**
* Actual data
*
* @return
*/
void readSheetDataList(List<ReadSheet> readSheetDataList);
Boolean needReadSheet();
void needReadSheet(Boolean needReadSheet);
Integer readSheetIndex();
void readSheetIndex(Integer readSheetIndex);
CellData tempCellData();
void tempCellData(CellData tempCellData);
Integer tempObjectIndex();
void tempObjectIndex(Integer tempObjectIndex);
Map<Integer, String> objectCacheMap();
}

21
src/main/java/com/alibaba/excel/enums/RowTypeEnum.java

@ -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,;
}

33
src/main/java/com/alibaba/excel/metadata/AbstractCell.java

@ -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;
}
}

22
src/main/java/com/alibaba/excel/metadata/Cell.java

@ -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();
}

38
src/main/java/com/alibaba/excel/metadata/CellData.java

@ -12,7 +12,7 @@ import com.alibaba.excel.util.StringUtils;
*
* @author Jiaju Zhuang
*/
public class CellData<T> {
public class CellData<T> extends AbstractCell {
private CellDataTypeEnum type;
/**
* {@link CellDataTypeEnum#NUMBER}
@ -226,6 +226,42 @@ public class CellData<T> {
}
}
public static CellData newEmptyInstance() {
return newEmptyInstance(null, null);
}
public static CellData newEmptyInstance(Integer rowIndex, Integer columnIndex) {
CellData cellData = new CellData(CellDataTypeEnum.EMPTY);
cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(columnIndex);
return cellData;
}
public static CellData newInstance(Boolean booleanValue) {
return newInstance(booleanValue, null, null);
}
public static CellData newInstance(Boolean booleanValue, Integer rowIndex, Integer columnIndex) {
CellData cellData = new CellData(booleanValue);
cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(columnIndex);
return cellData;
}
public static CellData newInstance(String stringValue, Integer rowIndex, Integer columnIndex) {
CellData cellData = new CellData(stringValue);
cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(columnIndex);
return cellData;
}
public static CellData newInstance(BigDecimal numberValue, Integer rowIndex, Integer columnIndex) {
CellData cellData = new CellData(numberValue);
cellData.setRowIndex(rowIndex);
cellData.setColumnIndex(columnIndex);
return cellData;
}
@Override
public String toString() {
if (type == null) {

12
src/main/java/com/alibaba/excel/metadata/CellExtra.java

@ -5,14 +5,14 @@ package com.alibaba.excel.metadata;
*
* @author Jiaju Zhuang
*/
public class CellExtra {
private String comment;
public class CellExtra extends AbstractCell {
private String note;
public String getComment() {
return comment;
public String getNote() {
return note;
}
public void setComment(String comment) {
this.comment = comment;
public void setNote(String note) {
this.note = note;
}
}

39
src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java

@ -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);
}

20
src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java

@ -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();
}

21
src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java

@ -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;
}
}

133
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -3,41 +3,27 @@ package com.alibaba.excel.read.metadata.holder;
import java.util.ArrayList;
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.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.metadata.AbstractHolder;
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.ModelBuildEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.listener.ReadListenerRegistryCenter;
import com.alibaba.excel.read.listener.event.AnalysisFinishEvent;
import com.alibaba.excel.read.metadata.ReadBasicParameter;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.ConverterUtils;
import com.alibaba.excel.util.StringUtils;
/**
* Read Holder
*
* @author Jiaju Zhuang
*/
public abstract class AbstractReadHolder extends AbstractHolder implements ReadHolder, ReadListenerRegistryCenter {
public abstract class AbstractReadHolder extends AbstractHolder implements ReadHolder {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractReadHolder.class);
/**
@ -117,123 +103,6 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
}
}
@Override
public void register(AnalysisEventListener listener) {
readListenerList.add(listener);
}
@Override
public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) {
Map<Integer, CellData> cellDataMap = event.getAnalysisResult();
if (CollectionUtils.isEmpty(cellDataMap)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.warn("Empty row!");
}
if (analysisContext.readWorkbookHolder().getIgnoreEmptyRow()) {
return;
}
}
ReadRowHolder readRowHolder = analysisContext.readRowHolder();
readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
int rowIndex = readRowHolder.getRowIndex();
int currentheadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();
if (rowIndex >= currentheadRowNumber) {
// Now is data
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
try {
readListener.invoke(readRowHolder.getCurrentRowAnalysisResult(), analysisContext);
} catch (Exception e) {
for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) {
try {
readListenerException.onException(e, analysisContext);
} catch (Exception exception) {
throw new ExcelAnalysisException(exception.getMessage(), exception);
}
}
break;
}
if (!readListener.hasNext(analysisContext)) {
throw new ExcelAnalysisStopException();
}
}
} else {
// Last head column
if (currentheadRowNumber == rowIndex + 1) {
buildHead(analysisContext, cellDataMap);
}
// Now is header
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
try {
readListener.invokeHead(cellDataMap, analysisContext);
} catch (Exception e) {
for (ReadListener readListenerException : analysisContext.currentReadHolder().readListenerList()) {
try {
readListenerException.onException(e, analysisContext);
} catch (Exception exception) {
throw new ExcelAnalysisException(exception.getMessage(), exception);
}
}
break;
}
if (!readListener.hasNext(analysisContext)) {
throw new ExcelAnalysisStopException();
}
}
}
}
@Override
public void notifyAfterAllAnalysed(AnalysisContext analysisContext) {
for (ReadListener readListener : readListenerList) {
readListener.doAfterAllAnalysed(analysisContext);
}
}
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);
}
public List<ReadListener> getReadListenerList() {
return readListenerList;
}

32
src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java

@ -1,6 +1,10 @@
package com.alibaba.excel.read.metadata.holder;
import java.util.Map;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.Holder;
@ -14,7 +18,14 @@ public class ReadRowHolder implements Holder {
* Returns row index of a row in the sheet that contains this cell.Start form 0.
*/
private Integer rowIndex;
/**
* Row type
*/
private RowTypeEnum rowType;
/**
* Cell map
*/
private Map<Integer, Cell> cellMap;
/**
* The result of the previous listener
*/
@ -24,8 +35,9 @@ public class ReadRowHolder implements Holder {
*/
private GlobalConfiguration globalConfiguration;
public ReadRowHolder(Integer rowIndex, GlobalConfiguration globalConfiguration) {
public ReadRowHolder(Integer rowIndex, RowTypeEnum rowType, GlobalConfiguration globalConfiguration) {
this.rowIndex = rowIndex;
this.rowType = rowType;
this.globalConfiguration = globalConfiguration;
}
@ -53,6 +65,22 @@ public class ReadRowHolder implements Holder {
this.rowIndex = rowIndex;
}
public RowTypeEnum getRowType() {
return rowType;
}
public void setRowType(RowTypeEnum rowType) {
this.rowType = rowType;
}
public Map<Integer, Cell> getCellMap() {
return cellMap;
}
public void setCellMap(Map<Integer, Cell> cellMap) {
this.cellMap = cellMap;
}
@Override
public HolderEnum holderType() {
return HolderEnum.ROW;

15
src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java

@ -131,12 +131,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
*/
private POIFSFileSystem poifsFileSystem;
/**
* Excel 2003 cannot read specific sheet. It can only read sheet by sheet.So when you specify one sheet, you ignore
* the others.
*/
private Boolean ignoreRecord03;
public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook, null, readWorkbook.getConvertAllFiled());
this.readWorkbook = readWorkbook;
@ -197,7 +191,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.extraReadSet = readWorkbook.getExtraReadSet();
}
this.hasReadSheet = new HashSet<Integer>();
this.ignoreRecord03 = Boolean.FALSE;
this.password = readWorkbook.getPassword();
}
@ -329,14 +322,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.poifsFileSystem = poifsFileSystem;
}
public Boolean getIgnoreRecord03() {
return ignoreRecord03;
}
public void setIgnoreRecord03(Boolean ignoreRecord03) {
this.ignoreRecord03 = ignoreRecord03;
}
public String getPassword() {
return password;
}

26
src/main/java/com/alibaba/excel/read/processor/AnalysisEventProcessor.java

@ -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);
}

149
src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java

@ -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);
}
}

18
src/main/java/com/alibaba/excel/util/SheetUtils.java

@ -1,11 +1,9 @@
package com.alibaba.excel.util;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.context.XlsReadContext;
import com.alibaba.excel.read.metadata.ReadSheet;
/**
@ -23,17 +21,14 @@ public class SheetUtils {
*
* @param readSheet
* actual sheet
* @param parameterReadSheetList
* parameters
* @param readAll
* @param xlsReadContext
* @return
*/
public static ReadSheet match(ReadSheet readSheet, List<ReadSheet> parameterReadSheetList, Boolean readAll,
GlobalConfiguration globalConfiguration) {
if (readAll) {
public static ReadSheet match(ReadSheet readSheet, XlsReadContext xlsReadContext) {
if (xlsReadContext.readAll()) {
return readSheet;
}
for (ReadSheet parameterReadSheet : parameterReadSheetList) {
for (ReadSheet parameterReadSheet : xlsReadContext.readSheetList()) {
if (parameterReadSheet == null) {
continue;
}
@ -49,7 +44,8 @@ public class SheetUtils {
String parameterSheetName = parameterReadSheet.getSheetName();
if (!StringUtils.isEmpty(parameterSheetName)) {
boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim())
|| (parameterReadSheet.getAutoTrim() == null && globalConfiguration.getAutoTrim());
|| (parameterReadSheet.getAutoTrim() == null
&& xlsReadContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
if (autoTrim) {
parameterSheetName = parameterSheetName.trim();
}

1
update.md

@ -4,6 +4,7 @@
* 修复xls 用Map接收时多次接收会是同一个对象的bug
* 修复浮点型数据导入到excel 会丢失精度的bug
* 新增支持接收批注
* 如果是`RuntimeException`则不再封装对象
# 2.1.4

Loading…
Cancel
Save