Browse Source

新增支持接收批注

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
6ed614612a
  1. 37
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  2. 7
      src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java
  3. 39
      src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java
  4. 2
      src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java
  5. 202
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  6. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java
  7. 33
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java
  8. 6
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java
  9. 4
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java
  10. 15
      src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordHandler.java
  11. 2
      src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java
  12. 18
      src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java
  13. 2
      src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java
  14. 12
      src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java
  15. 22
      src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSstRecordHandler.java
  16. 8
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java
  17. 12
      src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java
  18. 4
      src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java
  19. 5
      src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java
  20. 2
      src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java
  21. 10
      src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java
  22. 10
      src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java
  23. 49
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  24. 126
      src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java
  25. 95
      src/main/java/com/alibaba/excel/context/XlsReadContext.java
  26. 30
      src/main/java/com/alibaba/excel/context/xls/DefaultXlsReadContext.java
  27. 26
      src/main/java/com/alibaba/excel/context/xls/XlsReadContext.java
  28. 30
      src/main/java/com/alibaba/excel/context/xlsx/DefaultXlsxReadContext.java
  29. 28
      src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java
  30. 75
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java
  31. 112
      src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java
  32. 95
      src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java
  33. 16
      src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java
  34. 52
      src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java
  35. 2
      src/main/java/com/alibaba/excel/util/SheetUtils.java

37
src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java

@ -15,13 +15,15 @@ import com.alibaba.excel.analysis.v03.XlsSaxAnalyser;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl; import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.context.DefaultXlsReadContext; import com.alibaba.excel.context.xls.DefaultXlsReadContext;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
@ -58,7 +60,6 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
private void choiceExcelExecutor(ReadWorkbook readWorkbook) throws Exception { private void choiceExcelExecutor(ReadWorkbook readWorkbook) throws Exception {
ExcelTypeEnum excelType = ExcelTypeEnum excelType =
ExcelTypeEnum.valueOf(readWorkbook.getFile(), readWorkbook.getInputStream(), readWorkbook.getExcelType()); ExcelTypeEnum.valueOf(readWorkbook.getFile(), readWorkbook.getInputStream(), readWorkbook.getExcelType());
readWorkbook.setExcelType(excelType);
switch (excelType) { switch (excelType) {
case XLS: case XLS:
POIFSFileSystem poifsFileSystem; POIFSFileSystem poifsFileSystem;
@ -73,7 +74,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
try { try {
decryptedStream = DocumentFactoryHelper decryptedStream = DocumentFactoryHelper
.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword()); .getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword());
analysisContext = new AnalysisContextImpl(readWorkbook); analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream);
return; return;
} finally { } finally {
@ -86,15 +87,17 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (readWorkbook.getPassword() != null) { if (readWorkbook.getPassword() != null) {
Biff8EncryptionKey.setCurrentUserPassword(readWorkbook.getPassword()); Biff8EncryptionKey.setCurrentUserPassword(readWorkbook.getPassword());
} }
XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook); XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook, ExcelTypeEnum.XLS);
xlsReadContext.xlsReadWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
analysisContext = xlsReadContext; analysisContext = xlsReadContext;
excelReadExecutor = new XlsSaxAnalyser(xlsReadContext, poifsFileSystem); excelReadExecutor = new XlsSaxAnalyser(xlsReadContext);
break; break;
case XLSX: case XLSX:
analysisContext = new AnalysisContextImpl(readWorkbook); analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null); excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null);
break; break;
default: default:
break;
} }
} }
@ -104,19 +107,15 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
if (!readAll && CollectionUtils.isEmpty(readSheetList)) { if (!readAll && CollectionUtils.isEmpty(readSheetList)) {
throw new IllegalArgumentException("Specify at least one read sheet."); throw new IllegalArgumentException("Specify at least one read sheet.");
} }
analysisContext.readWorkbookHolder().setParametersheetDataList(readSheetList);
analysisContext.readWorkbookHolder().setReadAll(readAll);
try { try {
excelReadExecutor.execute(readSheetList, readAll); excelReadExecutor.execute();
} catch (ExcelAnalysisStopException e) { } catch (ExcelAnalysisStopException e) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Custom stop!"); LOGGER.debug("Custom stop!");
} }
} }
// The last sheet is read
if (excelReadExecutor instanceof XlsSaxAnalyser) {
if (analysisContext.readSheetHolder() != null) {
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext);
}
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
finish(); finish();
throw e; throw e;
@ -147,15 +146,17 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
throwable = t; throwable = t;
} }
try { try {
if (readWorkbookHolder.getOpcPackage() != null) { if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder)
readWorkbookHolder.getOpcPackage().revert(); && ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage() != null) {
((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage().revert();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwable = t; throwable = t;
} }
try { try {
if (readWorkbookHolder.getPoifsFileSystem() != null) { if ((readWorkbookHolder instanceof XlsReadWorkbookHolder)
readWorkbookHolder.getPoifsFileSystem().close(); && ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem() != null) {
((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem().close();
} }
} catch (Throwable t) { } catch (Throwable t) {
throwable = t; throwable = t;

7
src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java

@ -20,11 +20,6 @@ public interface ExcelReadExecutor {
/** /**
* Read the sheet. * Read the sheet.
*
* @param readSheetList
* Which sheets you need to read.
* @param readAll
* The <code>readSheetList</code> parameter is ignored, and all sheets are read.
*/ */
void execute(List<ReadSheet> readSheetList, Boolean readAll); void execute();
} }

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

@ -1,8 +1,8 @@
package com.alibaba.excel.analysis.v03; package com.alibaba.excel.analysis.v03;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.Map;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
@ -10,13 +10,14 @@ import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener; import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler; import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.analysis.v03.handlers.BoundSheetRecordHandler;
import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet;
/** /**
* In some cases, you need to know the number of sheets in advance and only read the file once in advance. * In some cases, you need to know the number of sheets in advance and only read the file once in advance.
@ -24,25 +25,28 @@ import com.alibaba.excel.read.metadata.ReadSheet;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class XlsListSheetListener implements HSSFListener { public class XlsListSheetListener implements HSSFListener {
private POIFSFileSystem poifsFileSystem; private XlsReadContext xlsReadContext;
private BofRecordHandler bofRecordHandler; private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>();
private static fin static {
XLS_RECORD_HANDLER_MAP.put(BOFRecord.sid, new BofRecordHandler());
XLS_RECORD_HANDLER_MAP.put(BoundSheetRecord.sid, new BoundSheetRecordHandler());
}
public XlsListSheetListener(XlsReadContext analysisContext, POIFSFileSystem poifsFileSystem) { public XlsListSheetListener(XlsReadContext xlsReadContext) {
this.poifsFileSystem = poifsFileSystem; this.xlsReadContext = xlsReadContext;
sheetList = new ArrayList<ReadSheet>();
bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false, false);
bofRecordHandler.init();
bofRecordHandler.init(null, true);
} }
@Override @Override
public void processRecord(Record record) { public void processRecord(Record record) {
bofRecordHandler.processRecord(record); XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid());
if (handler == null) {
return;
}
handler.processRecord(xlsReadContext, record);
} }
public List<ReadSheet> getSheetList() { public void execute() {
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
HSSFListener formatListener = new FormatTrackingHSSFListener(listener); HSSFListener formatListener = new FormatTrackingHSSFListener(listener);
HSSFEventFactory factory = new HSSFEventFactory(); HSSFEventFactory factory = new HSSFEventFactory();
@ -51,10 +55,9 @@ public class XlsListSheetListener implements HSSFListener {
new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener);
request.addListenerForAllRecords(workbookBuildingListener); request.addListenerForAllRecords(workbookBuildingListener);
try { try {
factory.processWorkbookEvents(request, poifsFileSystem); factory.processWorkbookEvents(request, xlsReadContext.xlsReadWorkbookHolder().getPoifsFileSystem());
} catch (IOException e) { } catch (IOException e) {
throw new ExcelAnalysisException(e); throw new ExcelAnalysisException(e);
} }
return sheetList;
} }
} }

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

@ -2,7 +2,7 @@ package com.alibaba.excel.analysis.v03;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Intercepts handle xls reads. * Intercepts handle xls reads.

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

@ -1,8 +1,6 @@
package com.alibaba.excel.analysis.v03; package com.alibaba.excel.analysis.v03;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -16,33 +14,46 @@ import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord; import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.record.TextObjectRecord;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.ExcelReadExecutor; 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.BlankRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler; import com.alibaba.excel.analysis.v03.handlers.BofRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BoolErrRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.BoundSheetRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.DummyRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.EofRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.FormulaRecordHandler; import com.alibaba.excel.analysis.v03.handlers.FormulaRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.IndexRecordHandler; import com.alibaba.excel.analysis.v03.handlers.IndexRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.LabelRecordHandler; import com.alibaba.excel.analysis.v03.handlers.LabelRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.MissingCellDummyRecordHandler; import com.alibaba.excel.analysis.v03.handlers.LabelSstRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.NoteRecordHandler; import com.alibaba.excel.analysis.v03.handlers.NoteRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.NumberRecordHandler; import com.alibaba.excel.analysis.v03.handlers.NumberRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.ObjRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.RkRecordHandler; import com.alibaba.excel.analysis.v03.handlers.RkRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.SstRecordHandler; import com.alibaba.excel.analysis.v03.handlers.SstRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.StringRecordHandler;
import com.alibaba.excel.analysis.v03.handlers.TextObjectRecordHandler; import com.alibaba.excel.analysis.v03.handlers.TextObjectRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.util.CollectionUtils;
/** /**
* /** * A text extractor for Excel files. * * /** * A text extractor for Excel files. *
@ -61,169 +72,78 @@ import com.alibaba.excel.util.CollectionUtils;
*/ */
public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor { public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class); private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
// private static final short DUMMY_RECORD_SID = -1;
// 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 XlsReadContext xlsReadContext;
private POIFSFileSystem poifsFileSystem;
private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32);
static { static {
XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler()); XLS_RECORD_HANDLER_MAP.put(BlankRecord.sid, new BlankRecordHandler());
} XLS_RECORD_HANDLER_MAP.put(BOFRecord.sid, new BofRecordHandler());
XLS_RECORD_HANDLER_MAP.put(BoolErrRecord.sid, new BoolErrRecordHandler());
public XlsSaxAnalyser(XlsReadContext xlsReadContext, POIFSFileSystem poifsFileSystem) { XLS_RECORD_HANDLER_MAP.put(BoundSheetRecord.sid, new BoundSheetRecordHandler());
XLS_RECORD_HANDLER_MAP.put(DUMMY_RECORD_SID, new DummyRecordHandler());
XLS_RECORD_HANDLER_MAP.put(EOFRecord.sid, new EofRecordHandler());
XLS_RECORD_HANDLER_MAP.put(FormulaRecord.sid, new FormulaRecordHandler());
XLS_RECORD_HANDLER_MAP.put(IndexRecord.sid, new IndexRecordHandler());
XLS_RECORD_HANDLER_MAP.put(LabelRecord.sid, new LabelRecordHandler());
XLS_RECORD_HANDLER_MAP.put(LabelSSTRecord.sid, new LabelSstRecordHandler());
XLS_RECORD_HANDLER_MAP.put(NoteRecord.sid, new NoteRecordHandler());
XLS_RECORD_HANDLER_MAP.put(NumberRecord.sid, new NumberRecordHandler());
XLS_RECORD_HANDLER_MAP.put(ObjRecord.sid, new ObjRecordHandler());
XLS_RECORD_HANDLER_MAP.put(RKRecord.sid, new RkRecordHandler());
XLS_RECORD_HANDLER_MAP.put(SSTRecord.sid, new SstRecordHandler());
XLS_RECORD_HANDLER_MAP.put(StringRecord.sid, new StringRecordHandler());
XLS_RECORD_HANDLER_MAP.put(TextObjectRecord.sid, new TextObjectRecordHandler());
}
public XlsSaxAnalyser(XlsReadContext xlsReadContext) {
this.xlsReadContext = xlsReadContext; this.xlsReadContext = xlsReadContext;
this.poifsFileSystem = poifsFileSystem;
xlsReadContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem);
} }
@Override @Override
public List<ReadSheet> sheetList() { public List<ReadSheet> sheetList() {
if (xlsReadContext.readSheetDataList() == null) { if (xlsReadContext.readWorkbookHolder().getActualSheetDataList() == null) {
earlySheetDataList();
LOGGER.warn("Getting the 'sheetList' before reading will cause the file to be read twice."); LOGGER.warn("Getting the 'sheetList' before reading will cause the file to be read twice.");
XlsListSheetListener xlsListSheetListener = new XlsListSheetListener(xlsReadContext, poifsFileSystem); new XlsListSheetListener(xlsReadContext).execute();
}
return xlsReadContext.readSheetDataList();
} }
return xlsReadContext.readWorkbookHolder().getActualSheetDataList();
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 @Override
public void execute() { public void execute() {
this.readAll = readAll; XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
this.readSheetList = readSheetList;
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener); xlsReadWorkbookHolder.setFormatTrackingHSSFListener(new FormatTrackingHSSFListener(listener));
workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener =
if (workbookBuildingListener != null && stubWorkbook == null) { new EventWorkbookBuilder.SheetRecordCollectingListener(
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook(); xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
} xlsReadWorkbookHolder.setHsffWorkbook(workbookBuildingListener.getStubHSSFWorkbook());
init();
HSSFEventFactory factory = new HSSFEventFactory(); HSSFEventFactory factory = new HSSFEventFactory();
HSSFRequest request = new HSSFRequest(); HSSFRequest request = new HSSFRequest();
request.addListenerForAllRecords(formatListener); request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener());
try { try {
factory.processWorkbookEvents(request, poifsFileSystem); factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem());
} catch (IOException e) { } catch (IOException e) {
throw new ExcelAnalysisException(e); throw new ExcelAnalysisException(e);
} }
// Sometimes tables lack the end record of the last column // Sometimes tables lack the end record of the last column
if (!xlsReadContext.cellMap().isEmpty()) { if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) {
endRow(); // Forge a termination data
} processRecord(new LastCellOfRowDummyRecord(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1, -1));
} }
private void init() {
recordHandlerMap = new HashMap<Short, XlsRecordHandler>(32);
buildXlsRecordHandlers();
} }
@Override @Override
public void processRecord(Record record) { public void processRecord(Record record) {
XlsRecordHandler handler = recordHandlerMap.get(record.getSid()); XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid());
if ((handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.readWorkbookHolder().getIgnoreRecord03()) { if (handler == null) {
return;
}
if ((handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.xlsReadSheetHolder().getIgnoreRecord()) {
// No need to read the current sheet // No need to read the current sheet
return; return;
} }
handler.processRecord(xlsReadContext, record); handler.processRecord(xlsReadContext, record);
int thisRow = -1;
int thisColumn = -1;
CellData cellData = null;
for (XlsRecordHandler handler : this.recordHandlers) {
if (handler.support(record)) {
handler.processRecord(record);
thisRow = handler.getRow();
thisColumn = handler.getColumn();
cellData = handler.getCellData();
if (cellData != null) {
cellData.checkEmpty();
if (CellDataTypeEnum.EMPTY != cellData.getType()) {
records.put(thisColumn, cellData);
}
}
break;
}
}
// If we got something to print out, do so
if (cellData != null && xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()
&& CellDataTypeEnum.STRING == cellData.getType()) {
cellData.setStringValue(cellData.getStringValue().trim());
}
processLastCellOfRow(record);
}
private boolean ignoreRecord(Record record) {
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() {
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(xlsReadContext));
// The table has been counted and there are no duplicate statistics
if (sheets == null) {
sheets = new ArrayList<ReadSheet>();
recordHandlers.add(new BofRecordHandler(xlsReadContext, sheets, false, true));
} else {
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);
}
for (XlsRecordHandler x : recordHandlers) {
x.init();
if (x instanceof BofRecordHandler) {
BofRecordHandler bofRecordHandler = (BofRecordHandler)x;
bofRecordHandler.init(readSheetList, readAll);
}
}
}
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
/** /**
@ -17,6 +17,7 @@ public class BlankRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
BlankRecord br = (BlankRecord)record; BlankRecord br = (BlankRecord)record;
xlsReadContext.cellMap().put((int)br.getColumn(), CellData.newEmptyInstance(br.getRow(), (int)br.getColumn())); xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)br.getColumn(),
CellData.newEmptyInstance(br.getRow(), (int)br.getColumn()));
} }
} }

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

@ -8,9 +8,11 @@ import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.XlsRecordHandler; import com.alibaba.excel.analysis.v03.XlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.util.SheetUtils; import com.alibaba.excel.util.SheetUtils;
/** /**
@ -26,43 +28,44 @@ public class BofRecordHandler implements XlsRecordHandler {
if (br.getType() != BOFRecord.TYPE_WORKSHEET) { if (br.getType() != BOFRecord.TYPE_WORKSHEET) {
return; return;
} }
XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder();
XlsReadSheetHolder XlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
// Init read sheet Data // Init read sheet Data
initReadSheetDataList(xlsReadContext); initReadSheetDataList(xlsReadWorkbookHolder);
Integer readSheetIndex = xlsReadContext.readSheetIndex(); Integer readSheetIndex = xlsReadWorkbookHolder.getReadSheetIndex();
if (readSheetIndex == null) { if (readSheetIndex == null) {
readSheetIndex = 0; readSheetIndex = 0;
xlsReadContext.readSheetIndex(readSheetIndex); xlsReadWorkbookHolder.setReadSheetIndex(readSheetIndex);
} }
ReadSheet readSheet = xlsReadWorkbookHolder.getActualSheetDataList().get(readSheetIndex);
ReadSheet readSheet = xlsReadContext.readSheetDataList().get(readSheetIndex);
assert readSheet != null : "Can't find the sheet."; assert readSheet != null : "Can't find the sheet.";
// Copy the parameter to the current sheet // Copy the parameter to the current sheet
readSheet = SheetUtils.match(readSheet, xlsReadContext); readSheet = SheetUtils.match(readSheet, xlsReadContext);
if (readSheet != null) { if (readSheet != null) {
xlsReadContext.currentSheet0(readSheet); xlsReadContext.currentSheet(readSheet);
xlsReadContext.ignoreRecord(Boolean.FALSE); XlsReadSheetHolder.setIgnoreRecord(Boolean.FALSE);
} else { } else {
xlsReadContext.ignoreRecord(Boolean.TRUE); XlsReadSheetHolder.setIgnoreRecord(Boolean.TRUE);
} }
// Go read the next one // Go read the next one
xlsReadContext.readSheetIndex(xlsReadContext.readSheetIndex() + 1); xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1);
} }
private void initReadSheetDataList(XlsReadContext xlsReadContext) { private void initReadSheetDataList(XlsReadWorkbookHolder xlsReadWorkbookHolder) {
if (xlsReadContext.readSheetDataList() != null) { if (xlsReadWorkbookHolder.getActualSheetDataList() != null) {
return; return;
} }
BoundSheetRecord[] boundSheetRecords = BoundSheetRecord[] boundSheetRecords =
BoundSheetRecord.orderByBofPosition(xlsReadContext.boundSheetRecordList()); BoundSheetRecord.orderByBofPosition(xlsReadWorkbookHolder.getBoundSheetRecordList());
List<ReadSheet> readSheetDataList = new ArrayList<ReadSheet>(); List<ReadSheet> readSheetDataList = new ArrayList<ReadSheet>();
for (int i = 0; i < boundSheetRecords.length; i++) { for (int i = 0; i < boundSheetRecords.length; i++) {
BoundSheetRecord boundSheetRecord = boundSheetRecords[i]; BoundSheetRecord boundSheetRecord = boundSheetRecords[i];
ReadSheet readSheet = new ReadSheet(i, boundSheetRecord.getSheetname()); ReadSheet readSheet = new ReadSheet(i, boundSheetRecord.getSheetname());
readSheetDataList.add(readSheet); readSheetDataList.add(readSheet);
} }
xlsReadContext.readSheetDataList(readSheetDataList); xlsReadWorkbookHolder.setActualSheetDataList(readSheetDataList);
// Just need to get the list of sheets // Just need to get the list of sheets
if (!xlsReadContext.needReadSheet()) { if (!xlsReadWorkbookHolder.getNeedReadSheet()) {
throw new ExcelAnalysisStopException("Just need to get the list of sheets."); throw new ExcelAnalysisStopException("Just need to get the list of sheets.");
} }
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -18,8 +18,8 @@ public class BoolErrRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
BoolErrRecord ber = (BoolErrRecord)record; BoolErrRecord ber = (BoolErrRecord)record;
xlsReadContext.cellMap().put((int)ber.getColumn(), xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)ber.getColumn(),
CellData.newInstance(ber.getBooleanValue(), ber.getRow(), (int)ber.getColumn())); CellData.newInstance(ber.getBooleanValue(), ber.getRow(), (int)ber.getColumn()));
xlsReadContext.tempRowType(RowTypeEnum.DATA); xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA);
} }
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Record handler * Record handler
@ -16,6 +16,6 @@ public class BoundSheetRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
BoundSheetRecord bsr = (BoundSheetRecord)record; BoundSheetRecord bsr = (BoundSheetRecord)record;
xlsReadContext.boundSheetRecordList().add((BoundSheetRecord)record); xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add((BoundSheetRecord)record);
} }
} }

15
src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java → src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordHandler.java

@ -7,30 +7,33 @@ import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.metadata.Cell; import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
/** /**
* Record handler * Record handler
* *
* @author Dan Zheng * @author Dan Zheng
*/ */
public class DummyRecordRecordHandler implements IgnorableXlsRecordHandler { public class DummyRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
if (record instanceof LastCellOfRowDummyRecord) { if (record instanceof LastCellOfRowDummyRecord) {
// End of this row // End of this row
LastCellOfRowDummyRecord lcrdr = (LastCellOfRowDummyRecord)record; LastCellOfRowDummyRecord lcrdr = (LastCellOfRowDummyRecord)record;
xlsReadContext.rowIndex(lcrdr.getRow()); xlsReadSheetHolder.setRowIndex(lcrdr.getRow());
xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadContext.tempRowType(), xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadSheetHolder.getTempRowType(),
xlsReadContext.readSheetHolder().getGlobalConfiguration())); xlsReadContext.readSheetHolder().getGlobalConfiguration()));
xlsReadContext.analysisEventProcessor().endRow(xlsReadContext); xlsReadContext.analysisEventProcessor().endRow(xlsReadContext);
xlsReadContext.cellMap(new LinkedHashMap<Integer, Cell>()); xlsReadSheetHolder.setCellMap(new LinkedHashMap<Integer, Cell>());
} else if (record instanceof MissingCellDummyRecord) { } else if (record instanceof MissingCellDummyRecord) {
MissingCellDummyRecord mcdr = (MissingCellDummyRecord)record; MissingCellDummyRecord mcdr = (MissingCellDummyRecord)record;
xlsReadContext.cellMap().put(mcdr.getColumn(), CellData.newEmptyInstance(mcdr.getRow(), mcdr.getColumn())); xlsReadSheetHolder.getCellMap().put(mcdr.getColumn(),
CellData.newEmptyInstance(mcdr.getRow(), mcdr.getColumn()));
} }
} }
} }

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

@ -3,7 +3,7 @@ package com.alibaba.excel.analysis.v03.handlers;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Record handler * Record handler

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

@ -1,6 +1,7 @@
package com.alibaba.excel.analysis.v03.handlers; package com.alibaba.excel.analysis.v03.handlers;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Map;
import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.FormulaRecord;
@ -10,8 +11,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
/** /**
@ -26,13 +28,15 @@ public class FormulaRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
FormulaRecord frec = (FormulaRecord)record; FormulaRecord frec = (FormulaRecord)record;
Map<Integer, Cell> cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap();
CellData tempCellData = new CellData(); CellData tempCellData = new CellData();
tempCellData.setRowIndex(frec.getRow()); tempCellData.setRowIndex(frec.getRow());
tempCellData.setColumnIndex((int)frec.getColumn()); tempCellData.setColumnIndex((int)frec.getColumn());
CellType cellType = CellType.forInt(frec.getCachedResultType()); CellType cellType = CellType.forInt(frec.getCachedResultType());
String formulaValue = null; String formulaValue = null;
try { try {
formulaValue = HSSFFormulaParser.toFormulaString(xlsReadContext.hsffWorkbook(), frec.getParsedExpression()); formulaValue = HSSFFormulaParser.toFormulaString(xlsReadContext.xlsReadWorkbookHolder().getHsffWorkbook(),
frec.getParsedExpression());
} catch (Exception e) { } catch (Exception e) {
LOGGER.debug("Get formula value error.", e); LOGGER.debug("Get formula value error.", e);
} }
@ -43,26 +47,26 @@ public class FormulaRecordHandler implements IgnorableXlsRecordHandler {
// Formula result is a string // Formula result is a string
// This is stored in the next record // This is stored in the next record
tempCellData.setType(CellDataTypeEnum.STRING); tempCellData.setType(CellDataTypeEnum.STRING);
xlsReadContext.tempCellData(tempCellData); xlsReadContext.xlsReadSheetHolder().setTempCellData(tempCellData);
break; break;
case NUMERIC: case NUMERIC:
tempCellData.setType(CellDataTypeEnum.NUMBER); tempCellData.setType(CellDataTypeEnum.NUMBER);
tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue())); tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue()));
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); cellMap.put((int)frec.getColumn(), tempCellData);
break; break;
case ERROR: case ERROR:
tempCellData.setType(CellDataTypeEnum.ERROR); tempCellData.setType(CellDataTypeEnum.ERROR);
tempCellData.setStringValue(ERROR); tempCellData.setStringValue(ERROR);
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); cellMap.put((int)frec.getColumn(), tempCellData);
break; break;
case BOOLEAN: case BOOLEAN:
tempCellData.setType(CellDataTypeEnum.BOOLEAN); tempCellData.setType(CellDataTypeEnum.BOOLEAN);
tempCellData.setBooleanValue(frec.getCachedBooleanValue()); tempCellData.setBooleanValue(frec.getCachedBooleanValue());
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); cellMap.put((int)frec.getColumn(), tempCellData);
break; break;
default: default:
tempCellData.setType(CellDataTypeEnum.EMPTY); tempCellData.setType(CellDataTypeEnum.EMPTY);
xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); cellMap.put((int)frec.getColumn(), tempCellData);
break; break;
} }
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.IndexRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Record handler * Record handler

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -17,8 +17,12 @@ public class LabelRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
LabelRecord lrec = (LabelRecord)record; LabelRecord lrec = (LabelRecord)record;
xlsReadContext.cellMap().put((int)lrec.getColumn(), String data = lrec.getValue();
CellData.newInstance(lrec.getValue(), lrec.getRow(), (int)lrec.getColumn())); if (data != null && xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
xlsReadContext.tempRowType(RowTypeEnum.DATA); data = data.trim();
}
xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)lrec.getColumn(),
CellData.newInstance(data, lrec.getRow(), (int)lrec.getColumn()));
xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA);
} }
} }

22
src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java → src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSstRecordHandler.java

@ -1,12 +1,15 @@
package com.alibaba.excel.analysis.v03.handlers; package com.alibaba.excel.analysis.v03.handlers;
import java.util.Map;
import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.cache.ReadCache; import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
/** /**
@ -14,25 +17,26 @@ import com.alibaba.excel.metadata.CellData;
* *
* @author Dan Zheng * @author Dan Zheng
*/ */
public class LabelSSTRecordHandler implements IgnorableXlsRecordHandler { public class LabelSstRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
LabelSSTRecord lsrec = (LabelSSTRecord)record; LabelSSTRecord lsrec = (LabelSSTRecord)record;
ReadCache readCache = xlsReadContext.readWorkbookHolder().getReadCache(); ReadCache readCache = xlsReadContext.readWorkbookHolder().getReadCache();
Map<Integer, Cell> cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap();
if (readCache == null) { if (readCache == null) {
xlsReadContext.cellMap().put((int)lsrec.getColumn(), cellMap.put((int)lsrec.getColumn(), CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn()));
CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn()));
return; return;
} }
String data = readCache.get(lsrec.getSSTIndex()); String data = readCache.get(lsrec.getSSTIndex());
if (data == null) { if (data == null) {
xlsReadContext.cellMap().put((int)lsrec.getColumn(), cellMap.put((int)lsrec.getColumn(), CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn()));
CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn()));
return; return;
} }
xlsReadContext.cellMap().put((int)lsrec.getColumn(), if (xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
CellData.newInstance(data, lsrec.getRow(), (int)lsrec.getColumn())); data = data.trim();
xlsReadContext.tempRowType(RowTypeEnum.DATA); }
cellMap.put((int)lsrec.getColumn(), CellData.newInstance(data, lsrec.getRow(), (int)lsrec.getColumn()));
xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA);
} }
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.metadata.CellExtra;
@ -18,12 +18,12 @@ public class NoteRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
NoteRecord nr = (NoteRecord)record; NoteRecord nr = (NoteRecord)record;
String note = xlsReadContext.objectCacheMap().get(nr.getShapeId()); String note = xlsReadContext.xlsReadSheetHolder().getObjectCacheMap().get(nr.getShapeId());
CellExtra cellExtra = new CellExtra(); CellExtra cellExtra = new CellExtra();
cellExtra.setRowIndex(nr.getRow()); cellExtra.setRowIndex(nr.getRow());
cellExtra.setRowIndex(nr.getColumn()); cellExtra.setRowIndex(nr.getColumn());
cellExtra.setNote(note); cellExtra.setNote(note);
xlsReadContext.cellMap().put(nr.getColumn(), cellExtra); xlsReadContext.xlsReadSheetHolder().getCellMap().put(nr.getColumn(), cellExtra);
xlsReadContext.tempRowType(RowTypeEnum.EXTRA); xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.EXTRA);
} }
} }

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

@ -7,7 +7,7 @@ import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
@ -22,12 +22,12 @@ public class NumberRecordHandler implements IgnorableXlsRecordHandler {
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
NumberRecord nr = (NumberRecord)record; NumberRecord nr = (NumberRecord)record;
CellData cellData = CellData.newInstance(BigDecimal.valueOf(nr.getValue()), nr.getRow(), (int)nr.getColumn()); CellData cellData = CellData.newInstance(BigDecimal.valueOf(nr.getValue()), nr.getRow(), (int)nr.getColumn());
Integer dataFormat = xlsReadContext.formatTrackingHSSFListener().getFormatIndex(nr); Integer dataFormat = xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatIndex(nr);
cellData.setDataFormat(dataFormat); cellData.setDataFormat(dataFormat);
cellData.setDataFormatString( cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat,
BuiltinFormats.getBuiltinFormat(dataFormat, xlsReadContext.formatTrackingHSSFListener().getFormatString(nr), xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatString(nr),
xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale())); xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale()));
xlsReadContext.cellMap().put((int)nr.getColumn(), cellData); xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)nr.getColumn(), cellData);
xlsReadContext.tempRowType(RowTypeEnum.DATA); xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA);
} }
} }

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

@ -6,7 +6,7 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SubRecord; import org.apache.poi.hssf.record.SubRecord;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Record handler * Record handler
@ -21,7 +21,7 @@ public class ObjRecordHandler implements IgnorableXlsRecordHandler {
if (subRecord instanceof CommonObjectDataSubRecord) { if (subRecord instanceof CommonObjectDataSubRecord) {
CommonObjectDataSubRecord codsr = (CommonObjectDataSubRecord)subRecord; CommonObjectDataSubRecord codsr = (CommonObjectDataSubRecord)subRecord;
if (CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT == codsr.getObjectType()) { if (CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT == codsr.getObjectType()) {
xlsReadContext.tempObjectIndex(codsr.getObjectId()); xlsReadContext.xlsReadSheetHolder().setTempObjectIndex(codsr.getObjectId());
} }
break; break;
} }

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

@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.Record;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
/** /**
@ -17,6 +17,7 @@ public class RkRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
RKRecord re = (RKRecord)record; RKRecord re = (RKRecord)record;
xlsReadContext.cellMap().put((int)re.getColumn(), CellData.newEmptyInstance(re.getRow(), (int)re.getColumn())); xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)re.getColumn(),
CellData.newEmptyInstance(re.getRow(), (int)re.getColumn()));
} }
} }

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

@ -5,7 +5,7 @@ import org.apache.poi.hssf.record.SSTRecord;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.cache.XlsCache; import com.alibaba.excel.cache.XlsCache;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
/** /**
* Record handler * Record handler

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

@ -6,8 +6,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
/** /**
* Record handler * Record handler
@ -21,13 +22,14 @@ public class StringRecordHandler implements IgnorableXlsRecordHandler {
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
// String for formula // String for formula
StringRecord srec = (StringRecord)record; StringRecord srec = (StringRecord)record;
CellData tempCellData = xlsReadContext.tempCellData(); XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
CellData tempCellData = xlsReadSheetHolder.getTempCellData();
if (tempCellData == null) { if (tempCellData == null) {
LOGGER.warn("String type formula but no value found."); LOGGER.warn("String type formula but no value found.");
return; return;
} }
tempCellData.setStringValue(srec.getString()); tempCellData.setStringValue(srec.getString());
xlsReadContext.cellMap().put(tempCellData.getColumnIndex(), tempCellData); xlsReadSheetHolder.getCellMap().put(tempCellData.getColumnIndex(), tempCellData);
xlsReadContext.tempCellData(null); xlsReadSheetHolder.setTempCellData(null);
} }
} }

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

@ -6,7 +6,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
/** /**
* Record handler * Record handler
@ -19,12 +20,13 @@ public class TextObjectRecordHandler implements IgnorableXlsRecordHandler {
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
TextObjectRecord tor = (TextObjectRecord)record; TextObjectRecord tor = (TextObjectRecord)record;
Integer tempObjectIndex = xlsReadContext.tempObjectIndex(); XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder();
Integer tempObjectIndex = xlsReadSheetHolder.getTempObjectIndex();
if (tempObjectIndex == null) { if (tempObjectIndex == null) {
LOGGER.debug("tempObjectIndex is null."); LOGGER.debug("tempObjectIndex is null.");
return; return;
} }
xlsReadContext.objectCacheMap().put(tempObjectIndex, tor.getStr().getString()); xlsReadSheetHolder.getObjectCacheMap().put(tempObjectIndex, tor.getStr().getString());
xlsReadContext.tempObjectIndex(null); xlsReadSheetHolder.setTempObjectIndex(null);
} }
} }

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

@ -1,6 +1,7 @@
package com.alibaba.excel.context; package com.alibaba.excel.context;
import java.io.InputStream; import java.io.InputStream;
import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -13,6 +14,10 @@ import com.alibaba.excel.read.metadata.holder.ReadHolder;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder; import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.read.processor.AnalysisEventProcessor; import com.alibaba.excel.read.processor.AnalysisEventProcessor;
import com.alibaba.excel.read.processor.DefalutAnalysisEventProcessor; import com.alibaba.excel.read.processor.DefalutAnalysisEventProcessor;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
@ -44,11 +49,20 @@ public class AnalysisContextImpl implements AnalysisContext {
*/ */
private AnalysisEventProcessor analysisEventProcessor; private AnalysisEventProcessor analysisEventProcessor;
public AnalysisContextImpl(ReadWorkbook readWorkbook) { public AnalysisContextImpl(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) {
if (readWorkbook == null) { if (readWorkbook == null) {
throw new IllegalArgumentException("Workbook argument cannot be null"); throw new IllegalArgumentException("Workbook argument cannot be null");
} }
readWorkbookHolder = new ReadWorkbookHolder(readWorkbook); switch (actualExcelType) {
case XLS:
readWorkbookHolder = new XlsReadWorkbookHolder(readWorkbook);
break;
case XLSX:
readWorkbookHolder = new XlsxReadWorkbookHolder(readWorkbook);
break;
default:
break;
}
currentReadHolder = readWorkbookHolder; currentReadHolder = readWorkbookHolder;
analysisEventProcessor = new DefalutAnalysisEventProcessor(); analysisEventProcessor = new DefalutAnalysisEventProcessor();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -58,7 +72,16 @@ public class AnalysisContextImpl implements AnalysisContext {
@Override @Override
public void currentSheet(ReadSheet readSheet) { public void currentSheet(ReadSheet readSheet) {
readSheetHolder = new ReadSheetHolder(readSheet, readWorkbookHolder); switch (readWorkbookHolder.getExcelType()) {
case XLS:
readSheetHolder = new XlsReadSheetHolder(readSheet, readWorkbookHolder);
break;
case XLSX:
readSheetHolder = new XlsxReadSheetHolder(readSheet, readWorkbookHolder);
break;
default:
break;
}
currentReadHolder = readSheetHolder; currentReadHolder = readSheetHolder;
if (readWorkbookHolder.getHasReadSheet().contains(readSheetHolder.getSheetNo())) { if (readWorkbookHolder.getHasReadSheet().contains(readSheetHolder.getSheetNo())) {
throw new ExcelAnalysisException("Cannot read sheet repeatedly."); throw new ExcelAnalysisException("Cannot read sheet repeatedly.");
@ -104,6 +127,26 @@ public class AnalysisContextImpl implements AnalysisContext {
return analysisEventProcessor; return analysisEventProcessor;
} }
@Override
public List<ReadSheet> readSheetList() {
return null;
}
@Override
public void readSheetList(List<ReadSheet> readSheetList) {
}
@Override
public Boolean readAll() {
return null;
}
@Override
public void readAll(Boolean readAll) {
}
@Override @Override
public Sheet getCurrentSheet() { public Sheet getCurrentSheet() {
Sheet sheet = new Sheet(readSheetHolder.getSheetNo() + 1); Sheet sheet = new Sheet(readSheetHolder.getSheetNo() + 1);

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

@ -1,126 +0,0 @@
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;
/**
*
* A context is the main anchorage point of a ls xls reader.
*
* @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;
}
}

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

@ -1,95 +0,0 @@
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.
*
* @author Jiaju Zhuang
*/
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();
}

30
src/main/java/com/alibaba/excel/context/xls/DefaultXlsReadContext.java

@ -0,0 +1,30 @@
package com.alibaba.excel.context.xls;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
/**
*
* A context is the main anchorage point of a ls xls reader.
*
* @author Jiaju Zhuang
*/
public class DefaultXlsReadContext extends AnalysisContextImpl implements XlsReadContext {
public DefaultXlsReadContext(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) {
super(readWorkbook, actualExcelType);
}
@Override
public XlsReadWorkbookHolder xlsReadWorkbookHolder() {
return (XlsReadWorkbookHolder)readWorkbookHolder();
}
@Override
public XlsReadSheetHolder xlsReadSheetHolder() {
return (XlsReadSheetHolder)readSheetHolder();
}
}

26
src/main/java/com/alibaba/excel/context/xls/XlsReadContext.java

@ -0,0 +1,26 @@
package com.alibaba.excel.context.xls;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
/**
* A context is the main anchorage point of a ls xls reader.
*
* @author Jiaju Zhuang
**/
public interface XlsReadContext extends AnalysisContext {
/**
* All information about the workbook you are currently working on.
*
* @return Current workbook holder
*/
XlsReadWorkbookHolder xlsReadWorkbookHolder();
/**
* All information about the sheet you are currently working on.
*
* @return Current sheet holder
*/
XlsReadSheetHolder xlsReadSheetHolder();
}

30
src/main/java/com/alibaba/excel/context/xlsx/DefaultXlsxReadContext.java

@ -0,0 +1,30 @@
package com.alibaba.excel.context.xlsx;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum;
/**
*
* A context is the main anchorage point of a ls xls reader.
*
* @author Jiaju Zhuang
*/
public class DefaultXlsxReadContext extends AnalysisContextImpl implements XlsxReadContext {
public DefaultXlsxReadContext(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) {
super(readWorkbook, actualExcelType);
}
@Override
public XlsxReadWorkbookHolder xlsxReadWorkbookHolder() {
return (XlsxReadWorkbookHolder)readWorkbookHolder();
}
@Override
public XlsxReadSheetHolder xlsxReadSheetHolder() {
return (XlsxReadSheetHolder)readSheetHolder();
}
}

28
src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java

@ -0,0 +1,28 @@
package com.alibaba.excel.context.xlsx;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
/**
* A context is the main anchorage point of a ls xlsx reader.
*
* @author Jiaju Zhuang
**/
public interface XlsxReadContext extends AnalysisContext {
/**
* All information about the workbook you are currently working on.
*
* @return Current workbook holder
*/
XlsxReadWorkbookHolder xlsxReadWorkbookHolder();
/**
* All information about the sheet you are currently working on.
*
* @return Current sheet holder
*/
XlsxReadSheetHolder xlsxReadSheetHolder();
}

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

@ -4,6 +4,7 @@ import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
@ -20,6 +21,7 @@ import com.alibaba.excel.enums.ExtraReadEnum;
import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
@ -86,23 +88,25 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
* Whether the encryption * Whether the encryption
*/ */
private String password; private String password;
/**
* SAXParserFactory used when reading xlsx.
* <p>
* The default will automatically find.
* <p>
* Please pass in the name of a class ,like : "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
*
* @see SAXParserFactory#newInstance()
* @see SAXParserFactory#newInstance(String, ClassLoader)
*/
private String xlsxSAXParserFactoryName;
/** /**
* Read some additional fields. None are read by default. * Read some additional fields. None are read by default.
* *
* @see ExtraReadEnum * @see ExtraReadEnum
*/ */
private Set<ExtraReadEnum> extraReadSet; private Set<ExtraReadEnum> extraReadSet;
/**
* Actual sheet data
*/
private List<ReadSheet> actualSheetDataList;
/**
* Parameter sheet data
*/
private List<ReadSheet> parametersheetDataList;
/**
* Read all
*/
private Boolean readAll;
/** /**
* The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a * The default is all excel objects.if true , you can use {@link com.alibaba.excel.annotation.ExcelIgnore} ignore a
* field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed. * field. if false , you must use {@link com.alibaba.excel.annotation.ExcelProperty} to use a filed.
@ -122,14 +126,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
* Prevent repeating sheet * Prevent repeating sheet
*/ */
private Set<Integer> hasReadSheet; private Set<Integer> hasReadSheet;
/**
* Package
*/
private OPCPackage opcPackage;
/**
* File System
*/
private POIFSFileSystem poifsFileSystem;
public ReadWorkbookHolder(ReadWorkbook readWorkbook) { public ReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook, null, readWorkbook.getConvertAllFiled()); super(readWorkbook, null, readWorkbook.getConvertAllFiled());
@ -184,7 +180,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
} else { } else {
this.defaultReturnMap = readWorkbook.getDefaultReturnMap(); this.defaultReturnMap = readWorkbook.getDefaultReturnMap();
} }
this.xlsxSAXParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName();
if (readWorkbook.getExtraReadSet() == null) { if (readWorkbook.getExtraReadSet() == null) {
this.extraReadSet = new HashSet<ExtraReadEnum>(); this.extraReadSet = new HashSet<ExtraReadEnum>();
} else { } else {
@ -306,44 +301,44 @@ public class ReadWorkbookHolder extends AbstractReadHolder {
this.defaultReturnMap = defaultReturnMap; this.defaultReturnMap = defaultReturnMap;
} }
public OPCPackage getOpcPackage() { public String getPassword() {
return opcPackage; return password;
} }
public void setOpcPackage(OPCPackage opcPackage) { public void setPassword(String password) {
this.opcPackage = opcPackage; this.password = password;
} }
public POIFSFileSystem getPoifsFileSystem() { public Set<ExtraReadEnum> getExtraReadSet() {
return poifsFileSystem; return extraReadSet;
} }
public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) { public void setExtraReadSet(Set<ExtraReadEnum> extraReadSet) {
this.poifsFileSystem = poifsFileSystem; this.extraReadSet = extraReadSet;
} }
public String getPassword() { public List<ReadSheet> getActualSheetDataList() {
return password; return actualSheetDataList;
} }
public void setPassword(String password) { public void setActualSheetDataList(List<ReadSheet> actualSheetDataList) {
this.password = password; this.actualSheetDataList = actualSheetDataList;
} }
public String getXlsxSAXParserFactoryName() { public List<ReadSheet> getParametersheetDataList() {
return xlsxSAXParserFactoryName; return parametersheetDataList;
} }
public void setXlsxSAXParserFactoryName(String xlsxSAXParserFactoryName) { public void setParametersheetDataList(List<ReadSheet> parametersheetDataList) {
this.xlsxSAXParserFactoryName = xlsxSAXParserFactoryName; this.parametersheetDataList = parametersheetDataList;
} }
public Set<ExtraReadEnum> getExtraReadSet() { public Boolean getReadAll() {
return extraReadSet; return readAll;
} }
public void setExtraReadSet(Set<ExtraReadEnum> extraReadSet) { public void setReadAll(Boolean readAll) {
this.extraReadSet = extraReadSet; this.readAll = readAll;
} }
@Override @Override

112
src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java

@ -0,0 +1,112 @@
package com.alibaba.excel.read.metadata.holder.xls;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
/**
* sheet holder
*
* @author Jiaju Zhuang
*/
public class XlsReadSheetHolder extends ReadSheetHolder {
/**
* Row type.Temporary storage, last set in <code>ReadRowHolder</code>.
*/
private RowTypeEnum tempRowType;
/**
* Data storage of the current row.
*/
private Map<Integer, Cell> cellMap;
/**
* Index of the current row.
*/
private Integer rowIndex;
/**
* Ignore record.
*/
private Boolean ignoreRecord;
/**
* Temp Cell Data.
*/
private CellData tempCellData;
/**
* Temp object index.
*/
private Integer tempObjectIndex;
/**
* Temp object index.
*/
private Map<Integer, String> objectCacheMap;
public XlsReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, readWorkbookHolder);
cellMap = new LinkedHashMap<Integer, Cell>();
ignoreRecord = Boolean.FALSE;
tempRowType = RowTypeEnum.EMPTY;
objectCacheMap = new HashMap<Integer, String>(16);
}
public RowTypeEnum getTempRowType() {
return tempRowType;
}
public void setTempRowType(RowTypeEnum tempRowType) {
this.tempRowType = tempRowType;
}
public Map<Integer, Cell> getCellMap() {
return cellMap;
}
public void setCellMap(Map<Integer, Cell> cellMap) {
this.cellMap = cellMap;
}
public Integer getRowIndex() {
return rowIndex;
}
public void setRowIndex(Integer rowIndex) {
this.rowIndex = rowIndex;
}
public Boolean getIgnoreRecord() {
return ignoreRecord;
}
public void setIgnoreRecord(Boolean ignoreRecord) {
this.ignoreRecord = ignoreRecord;
}
public CellData getTempCellData() {
return tempCellData;
}
public void setTempCellData(CellData tempCellData) {
this.tempCellData = tempCellData;
}
public Integer getTempObjectIndex() {
return tempObjectIndex;
}
public void setTempObjectIndex(Integer tempObjectIndex) {
this.tempObjectIndex = tempObjectIndex;
}
public Map<Integer, String> getObjectCacheMap() {
return objectCacheMap;
}
public void setObjectCacheMap(Map<Integer, String> objectCacheMap) {
this.objectCacheMap = objectCacheMap;
}
}

95
src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java

@ -0,0 +1,95 @@
package com.alibaba.excel.read.metadata.holder.xls;
import java.util.List;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
/**
* Workbook holder
*
* @author Jiaju Zhuang
*/
public class XlsReadWorkbookHolder extends ReadWorkbookHolder {
/**
* File System
*/
private POIFSFileSystem poifsFileSystem;
/**
* Format tracking HSSFListener
*/
private FormatTrackingHSSFListener formatTrackingHSSFListener;
/**
* HSSFWorkbook
*/
private HSSFWorkbook hsffWorkbook;
/**
* Bound sheet record list.
*/
private List<BoundSheetRecord> boundSheetRecordList;
/**
* Need read sheet.
*/
private Boolean needReadSheet;
/**
* Sheet Index
*/
private Integer readSheetIndex;
public XlsReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook);
}
public POIFSFileSystem getPoifsFileSystem() {
return poifsFileSystem;
}
public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) {
this.poifsFileSystem = poifsFileSystem;
}
public FormatTrackingHSSFListener getFormatTrackingHSSFListener() {
return formatTrackingHSSFListener;
}
public void setFormatTrackingHSSFListener(FormatTrackingHSSFListener formatTrackingHSSFListener) {
this.formatTrackingHSSFListener = formatTrackingHSSFListener;
}
public HSSFWorkbook getHsffWorkbook() {
return hsffWorkbook;
}
public void setHsffWorkbook(HSSFWorkbook hsffWorkbook) {
this.hsffWorkbook = hsffWorkbook;
}
public List<BoundSheetRecord> getBoundSheetRecordList() {
return boundSheetRecordList;
}
public void setBoundSheetRecordList(List<BoundSheetRecord> boundSheetRecordList) {
this.boundSheetRecordList = boundSheetRecordList;
}
public Boolean getNeedReadSheet() {
return needReadSheet;
}
public void setNeedReadSheet(Boolean needReadSheet) {
this.needReadSheet = needReadSheet;
}
public Integer getReadSheetIndex() {
return readSheetIndex;
}
public void setReadSheetIndex(Integer readSheetIndex) {
this.readSheetIndex = readSheetIndex;
}
}

16
src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java

@ -0,0 +1,16 @@
package com.alibaba.excel.read.metadata.holder.xlsx;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
/**
* sheet holder
*
* @author Jiaju Zhuang
*/
public class XlsxReadSheetHolder extends ReadSheetHolder {
public XlsxReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, readWorkbookHolder);
}
}

52
src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java

@ -0,0 +1,52 @@
package com.alibaba.excel.read.metadata.holder.xlsx;
import javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.opc.OPCPackage;
import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
/**
* Workbook holder
*
* @author Jiaju Zhuang
*/
public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
/**
* Package
*/
private OPCPackage opcPackage;
/**
* SAXParserFactory used when reading xlsx.
* <p>
* The default will automatically find.
* <p>
* Please pass in the name of a class ,like : "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
*
* @see SAXParserFactory#newInstance()
* @see SAXParserFactory#newInstance(String, ClassLoader)
*/
private String saxParserFactoryName;
public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook);
this.saxParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName();
}
public OPCPackage getOpcPackage() {
return opcPackage;
}
public void setOpcPackage(OPCPackage opcPackage) {
this.opcPackage = opcPackage;
}
public String getSaxParserFactoryName() {
return saxParserFactoryName;
}
public void setSaxParserFactoryName(String saxParserFactoryName) {
this.saxParserFactoryName = saxParserFactoryName;
}
}

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

@ -3,7 +3,7 @@ package com.alibaba.excel.util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.XlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
/** /**

Loading…
Cancel
Save