From f0d9051db20c8b26bec981d926e2ffddc887ed4a Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 27 Dec 2019 17:04:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E6=8E=A5?= =?UTF-8?q?=E6=94=B6=E6=89=B9=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v03/AbstractXlsRecordHandler.java | 39 ----- .../v03/IgnorableXlsRecordHandler.java | 8 + .../analysis/v03/XlsListSheetListener.java | 4 +- .../excel/analysis/v03/XlsRecordHandler.java | 49 +----- .../excel/analysis/v03/XlsSaxAnalyser.java | 139 ++++++++-------- .../handlers/BlankOrErrorRecordHandler.java | 52 ------ .../v03/handlers/BlankRecordHandler.java | 22 +++ .../v03/handlers/BofRecordHandler.java | 122 +++++--------- .../v03/handlers/BoolErrRecordHandler.java | 25 +++ .../v03/handlers/BoundSheetRecordHandler.java | 21 +++ .../handlers/DummyRecordRecordHandler.java | 36 +++++ .../v03/handlers/EofRecordHandler.java | 21 +++ .../v03/handlers/FormulaRecordHandler.java | 133 +++++----------- .../v03/handlers/IndexRecordHandler.java | 28 +--- .../v03/handlers/LabelRecordHandler.java | 33 +--- .../v03/handlers/LabelSSTRecordHandler.java | 38 +++++ .../MissingCellDummyRecordHandler.java | 43 ----- .../v03/handlers/NoteRecordHandler.java | 40 ++--- .../v03/handlers/NumberRecordHandler.java | 47 ++---- .../v03/handlers/ObjRecordHandler.java | 30 ++++ .../v03/handlers/RkRecordHandler.java | 32 +--- .../v03/handlers/SstRecordHandler.java | 44 +----- .../v03/handlers/StringRecordHandler.java | 33 ++++ .../v03/handlers/TextObjectRecordHandler.java | 37 ++--- .../excel/analysis/v07/XlsxRowHandler.java | 4 +- .../com/alibaba/excel/cache/XlsCache.java | 37 +++++ .../excel/context/AnalysisContext.java | 37 +++++ .../excel/context/AnalysisContextImpl.java | 12 ++ .../excel/context/DefaultXlsReadContext.java | 110 +++++++++++++ .../alibaba/excel/context/XlsReadContext.java | 84 ++++++++++ .../com/alibaba/excel/enums/RowTypeEnum.java | 21 +++ .../alibaba/excel/metadata/AbstractCell.java | 33 ++++ .../java/com/alibaba/excel/metadata/Cell.java | 22 +++ .../com/alibaba/excel/metadata/CellData.java | 38 ++++- .../com/alibaba/excel/metadata/CellExtra.java | 12 +- .../listener/ReadListenerRegistryCenter.java | 39 ----- .../listener/event/AnalysisFinishEvent.java | 20 --- .../event/EachRowAnalysisFinishEvent.java | 21 --- .../metadata/holder/AbstractReadHolder.java | 133 +--------------- .../read/metadata/holder/ReadRowHolder.java | 32 +++- .../metadata/holder/ReadWorkbookHolder.java | 15 -- .../processor/AnalysisEventProcessor.java | 26 +++ .../DefalutAnalysisEventProcessor.java | 149 ++++++++++++++++++ .../com/alibaba/excel/util/SheetUtils.java | 18 +-- update.md | 1 + 45 files changed, 1070 insertions(+), 870 deletions(-) delete mode 100644 src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/IgnorableXlsRecordHandler.java delete mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java delete mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java create mode 100644 src/main/java/com/alibaba/excel/cache/XlsCache.java create mode 100644 src/main/java/com/alibaba/excel/enums/RowTypeEnum.java create mode 100644 src/main/java/com/alibaba/excel/metadata/AbstractCell.java create mode 100644 src/main/java/com/alibaba/excel/metadata/Cell.java delete mode 100644 src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java delete mode 100644 src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java delete mode 100644 src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java create mode 100644 src/main/java/com/alibaba/excel/read/processor/AnalysisEventProcessor.java create mode 100644 src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java diff --git a/src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java deleted file mode 100644 index 0deba916..00000000 --- a/src/main/java/com/alibaba/excel/analysis/v03/AbstractXlsRecordHandler.java +++ /dev/null @@ -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(); - } -} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/IgnorableXlsRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/IgnorableXlsRecordHandler.java new file mode 100644 index 00000000..cb7e4a8c --- /dev/null +++ b/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 {} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java index bc93731d..906b3773 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java +++ b/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 sheetList; private BofRecordHandler bofRecordHandler; + private static fin + public XlsListSheetListener(XlsReadContext analysisContext, POIFSFileSystem poifsFileSystem) { this.poifsFileSystem = poifsFileSystem; sheetList = new ArrayList(); @@ -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) { diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java index b786bcb8..fca15b52 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java +++ b/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 { - /** - * 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); } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index edd763e8..68b0c474 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/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 readSheetList; + // /** + // * For parsing Formulas + // */ + // private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener; + // private FormatTrackingHSSFListener formatListener; + // private List sheets; + // private HSSFWorkbook stubWorkbook; + // private Map recordHandlerMap; + private XlsReadContext xlsReadContext; private POIFSFileSystem poifsFileSystem; - private Boolean readAll; - private List readSheetList; - private int lastRowNumber; - private int lastColumnNumber; - private int ii = 0; - - /** - * For parsing Formulas - */ - private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener; - private FormatTrackingHSSFListener formatListener; - private Map records; - private List sheets; - private HSSFWorkbook stubWorkbook; - private List recordHandlers = new ArrayList(); - private XlsReadContext analysisContext; - - public XlsSaxAnalyser(XlsReadContext context, POIFSFileSystem poifsFileSystem) { - this.analysisContext = context; - this.records = new LinkedHashMap(); + + private static final Map XLS_RECORD_HANDLER_MAP = new HashMap(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 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 sheets; + return xlsReadContext.readSheetDataList(); + } + + private void earlySheetDataList() { + LOGGER.warn("Getting the 'sheetList' before reading will cause the file to be read twice."); + + sheetList = new ArrayList(); + bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false, false); + bofRecordHandler.init(); + bofRecordHandler.init(null, true); + + XlsListSheetListener xlsListSheetListener = new XlsListSheetListener(xlsReadContext, poifsFileSystem); } @Override - public void execute(List 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(); + recordHandlerMap = new HashMap(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(); - 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(); - 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 BofRecordHandler(xlsReadContext, 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 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); } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java deleted file mode 100644 index 686ffb0b..00000000 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java new file mode 100644 index 00000000..24d99058 --- /dev/null +++ b/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())); + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java index af1a2b4e..5137d18a 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BofRecordHandler.java +++ b/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 boundSheetRecords = new ArrayList(); - private BoundSheetRecord[] orderedBsrs; - private int sheetIndex; - private List sheets; - private Boolean readAll; - private List readSheetList; - private boolean alreadyInit; - private boolean needInitSheet; - - public BofRecordHandler(XlsReadContext analysisContext, List sheets, boolean alreadyInit, - boolean needInitSheet) { - super(analysisContext); - this.sheets = sheets; - this.alreadyInit = alreadyInit; - this.needInitSheet = needInitSheet; - } +public class BofRecordHandler implements XlsRecordHandler { @Override - public boolean support(Record record) { - return BoundSheetRecord.sid == record.getSid() || BOFRecord.sid == record.getSid(); - } - - @Override - public void processRecord(Record record) { - if (record.getSid() == BoundSheetRecord.sid) { - boundSheetRecords.add((BoundSheetRecord)record); - } else if (record.getSid() == BOFRecord.sid) { - 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; - } - } - } - 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()); - 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++; - } + public void processRecord(XlsReadContext xlsReadContext, Record record) { + BOFRecord br = (BOFRecord)record; + if (br.getType() != BOFRecord.TYPE_WORKSHEET) { + return; } - } - - @Override - public void init() { - sheetIndex = 0; - orderedBsrs = null; - boundSheetRecords.clear(); - if (!alreadyInit) { - sheets.clear(); + // Init read sheet Data + initReadSheetDataList(xlsReadContext); + Integer readSheetIndex = xlsReadContext.readSheetIndex(); + if (readSheetIndex == null) { + readSheetIndex = 0; + xlsReadContext.readSheetIndex(readSheetIndex); } - } - public void init(List readSheetList, Boolean readAll) { - this.readSheetList = readSheetList; - this.readAll = readAll; + ReadSheet readSheet = xlsReadContext.readSheetDataList().get(readSheetIndex); + assert readSheet != null : "Can't find the sheet."; + // Copy the parameter to the current sheet + readSheet = SheetUtils.match(readSheet, xlsReadContext); + if (readSheet != null) { + xlsReadContext.currentSheet0(readSheet); + xlsReadContext.ignoreRecord(Boolean.FALSE); + } else { + xlsReadContext.ignoreRecord(Boolean.TRUE); + } + // Go read the next one + xlsReadContext.readSheetIndex(xlsReadContext.readSheetIndex() + 1); } - @Override - public int getOrder() { - return 0; + private void initReadSheetDataList(XlsReadContext xlsReadContext) { + if (xlsReadContext.readSheetDataList() != null) { + return; + } + BoundSheetRecord[] boundSheetRecords = + BoundSheetRecord.orderByBofPosition(xlsReadContext.boundSheetRecordList()); + List readSheetDataList = new ArrayList(); + 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."); + } } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java new file mode 100644 index 00000000..27d521c2 --- /dev/null +++ b/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); + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java new file mode 100644 index 00000000..809d7fd3 --- /dev/null +++ b/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); + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java new file mode 100644 index 00000000..53d4d355 --- /dev/null +++ b/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()); + } else if (record instanceof MissingCellDummyRecord) { + MissingCellDummyRecord mcdr = (MissingCellDummyRecord)record; + xlsReadContext.cellMap().put(mcdr.getColumn(), CellData.newEmptyInstance(mcdr.getRow(), mcdr.getColumn())); + } + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java new file mode 100644 index 00000000..9efe9885 --- /dev/null +++ b/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); + } + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java index b19e36d2..261b7e10 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/FormulaRecordHandler.java +++ b/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) { - FormulaRecord frec = (FormulaRecord)record; - this.row = frec.getRow(); - this.column = frec.getColumn(); - CellType cellType = CellType.forInt(frec.getCachedResultType()); - String formulaValue = null; - try { - formulaValue = HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()); - } catch (Exception e) { - LOGGER.warn("Get formula value error.{}", e.getMessage()); - } - 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); - break; - case NUMERIC: - this.cellData = new CellData(BigDecimal.valueOf(frec.getValue())); - this.cellData.setFormula(Boolean.TRUE); - this.cellData.setFormulaValue(formulaValue); - break; - case ERROR: - this.cellData = new CellData(CellDataTypeEnum.ERROR); - this.cellData.setStringValue(ERROR); - this.cellData.setFormula(Boolean.TRUE); - this.cellData.setFormulaValue(formulaValue); - break; - case BOOLEAN: - this.cellData = new CellData(frec.getCachedBooleanValue()); - this.cellData.setFormula(Boolean.TRUE); - this.cellData.setFormulaValue(formulaValue); - break; - default: - this.cellData = new CellData(CellDataTypeEnum.EMPTY); - this.cellData.setFormula(Boolean.TRUE); - this.cellData.setFormulaValue(formulaValue); - 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; - } + public void processRecord(XlsReadContext xlsReadContext, Record record) { + FormulaRecord frec = (FormulaRecord)record; + 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(xlsReadContext.hsffWorkbook(), frec.getParsedExpression()); + } catch (Exception e) { + 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 + tempCellData.setType(CellDataTypeEnum.STRING); + xlsReadContext.tempCellData(tempCellData); + break; + case NUMERIC: + tempCellData.setType(CellDataTypeEnum.NUMBER); + tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue())); + xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + break; + case ERROR: + tempCellData.setType(CellDataTypeEnum.ERROR); + tempCellData.setStringValue(ERROR); + xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + break; + case BOOLEAN: + tempCellData.setType(CellDataTypeEnum.BOOLEAN); + tempCellData.setBooleanValue(frec.getCachedBooleanValue()); + xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + break; + default: + tempCellData.setType(CellDataTypeEnum.EMPTY); + xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + break; } - } - - @Override - public void init() { - this.nextRow = 0; - this.nextColumn = 0; - } - - @Override - public int getOrder() { - return 0; } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java index 83105bf2..08a5a569 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/IndexRecordHandler.java +++ b/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()); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java index 79db34a2..93259e61 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelRecordHandler.java +++ b/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); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java new file mode 100644 index 00000000..5e08ff87 --- /dev/null +++ b/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); + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java deleted file mode 100644 index f40422ae..00000000 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/MissingCellDummyRecordHandler.java +++ /dev/null @@ -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; - } -} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java index f63d9eeb..255eb988 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NoteRecordHandler.java +++ b/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); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java index 948cf53d..381bacd4 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/NumberRecordHandler.java +++ b/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); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java new file mode 100644 index 00000000..5144a73f --- /dev/null +++ b/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; + } + } + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java index 08e2ac2b..cc902ef7 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/RkRecordHandler.java +++ b/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())); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java index 67962bdb..7f0f01d7 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/SstRecordHandler.java +++ b/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)); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java new file mode 100644 index 00000000..63e69536 --- /dev/null +++ b/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); + } +} diff --git a/src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java b/src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java index ee502dc3..764b69e6 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/TextObjectRecordHandler.java +++ b/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); - } - - @Override - public void init() { - - } +public class TextObjectRecordHandler implements IgnorableXlsRecordHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(TextObjectRecordHandler.class); @Override - public int getOrder() { - return 0; + public void processRecord(XlsReadContext xlsReadContext, Record record) { + TextObjectRecord tor = (TextObjectRecord)record; + Integer tempObjectIndex = xlsReadContext.tempObjectIndex(); + if (tempObjectIndex == null) { + LOGGER.debug("tempObjectIndex is null."); + return; + } + xlsReadContext.objectCacheMap().put(tempObjectIndex, tor.getStr().getString()); + xlsReadContext.tempObjectIndex(null); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java index b6702c1b..bc433cf6 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java +++ b/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; /** * diff --git a/src/main/java/com/alibaba/excel/cache/XlsCache.java b/src/main/java/com/alibaba/excel/cache/XlsCache.java new file mode 100644 index 00000000..9e814e0f --- /dev/null +++ b/src/main/java/com/alibaba/excel/cache/XlsCache.java @@ -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() {} + +} diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContext.java b/src/main/java/com/alibaba/excel/context/AnalysisContext.java index 66c69a52..b4414598 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContext.java +++ b/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 readSheetList(); + + /** + * Data that the customer needs to read + * + * @return + */ + void readSheetList(List readSheetList); + + /** + * Read all sheets + * + * @return + */ + Boolean readAll(); + + /** + * Read all sheets + * + * @return + */ + void readAll(Boolean readAll); + /** * get current sheet * diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index e5ded50f..9f03bcfa 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/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); diff --git a/src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java b/src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java index 3f7da09c..94878591 100644 --- a/src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java +++ b/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 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 boundSheetRecordList; public DefaultXlsReadContext(ReadWorkbook readWorkbook) { super(readWorkbook); + cellMap = new LinkedHashMap(); + rowType = RowTypeEnum.EMPTY; + boundSheetRecordList = new ArrayList(); + } + + @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 cellMap() { + return null; + } + + @Override + public void cellMap(Map 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(); + ignoreRecord = Boolean.FALSE; + rowType = RowTypeEnum.EMPTY; } } diff --git a/src/main/java/com/alibaba/excel/context/XlsReadContext.java b/src/main/java/com/alibaba/excel/context/XlsReadContext.java index a96009bb..7af83cbb 100644 --- a/src/main/java/com/alibaba/excel/context/XlsReadContext.java +++ b/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 cellMap(); + + void cellMap(Map 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 boundSheetRecordList(); + + void boundSheetRecordList(List boundSheetRecordList); + + /** + * Actual data + * + * @return + */ + List readSheetDataList(); + + /** + * Actual data + * + * @return + */ + void readSheetDataList(List 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 objectCacheMap(); } diff --git a/src/main/java/com/alibaba/excel/enums/RowTypeEnum.java b/src/main/java/com/alibaba/excel/enums/RowTypeEnum.java new file mode 100644 index 00000000..75dfc673 --- /dev/null +++ b/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,; +} diff --git a/src/main/java/com/alibaba/excel/metadata/AbstractCell.java b/src/main/java/com/alibaba/excel/metadata/AbstractCell.java new file mode 100644 index 00000000..5cea37cf --- /dev/null +++ b/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; + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/Cell.java b/src/main/java/com/alibaba/excel/metadata/Cell.java new file mode 100644 index 00000000..259df7af --- /dev/null +++ b/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(); +} diff --git a/src/main/java/com/alibaba/excel/metadata/CellData.java b/src/main/java/com/alibaba/excel/metadata/CellData.java index 7cd5496e..2124e103 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellData.java +++ b/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 { +public class CellData extends AbstractCell { private CellDataTypeEnum type; /** * {@link CellDataTypeEnum#NUMBER} @@ -226,6 +226,42 @@ public class CellData { } } + 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) { diff --git a/src/main/java/com/alibaba/excel/metadata/CellExtra.java b/src/main/java/com/alibaba/excel/metadata/CellExtra.java index 84e313db..3570eb96 100644 --- a/src/main/java/com/alibaba/excel/metadata/CellExtra.java +++ b/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; } } diff --git a/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java b/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java deleted file mode 100644 index 07bc68c2..00000000 --- a/src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java +++ /dev/null @@ -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); -} diff --git a/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java deleted file mode 100644 index 9749cf06..00000000 --- a/src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java +++ /dev/null @@ -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 getAnalysisResult(); -} diff --git a/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java b/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java deleted file mode 100644 index 879a59a7..00000000 --- a/src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java +++ /dev/null @@ -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 result; - - public EachRowAnalysisFinishEvent(Map content) { - this.result = content; - } - - @Override - public Map getAnalysisResult() { - return result; - } -} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java index 7133f995..a0bf7f1e 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java +++ b/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 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 cellDataMap) { - if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { - return; - } - Map dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext); - ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); - Map headMapData = excelHeadPropertyData.getHeadMap(); - Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); - Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); - Map tmpContentPropertyMap = - new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); - for (Map.Entry 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 headNameList = headData.getHeadNameList(); - String headName = headNameList.get(headNameList.size() - 1); - for (Map.Entry 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 getReadListenerList() { return readListenerList; } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java index 99160158..da745096 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java +++ b/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 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 getCellMap() { + return cellMap; + } + + public void setCellMap(Map cellMap) { + this.cellMap = cellMap; + } + @Override public HolderEnum holderType() { return HolderEnum.ROW; diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java index c03edf21..b97395ec 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/ReadWorkbookHolder.java +++ b/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(); - 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; } diff --git a/src/main/java/com/alibaba/excel/read/processor/AnalysisEventProcessor.java b/src/main/java/com/alibaba/excel/read/processor/AnalysisEventProcessor.java new file mode 100644 index 00000000..cd485382 --- /dev/null +++ b/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); +} diff --git a/src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java b/src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java new file mode 100644 index 00000000..e673913f --- /dev/null +++ b/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 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 cellDataMap) { + if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) { + return; + } + Map dataMap = ConverterUtils.convertToStringMap(cellDataMap, analysisContext); + ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty(); + Map headMapData = excelHeadPropertyData.getHeadMap(); + Map contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap(); + Map tmpHeadMap = new HashMap(headMapData.size() * 4 / 3 + 1); + Map tmpContentPropertyMap = + new HashMap(contentPropertyMapData.size() * 4 / 3 + 1); + for (Map.Entry 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 headNameList = headData.getHeadNameList(); + String headName = headNameList.get(headNameList.size() - 1); + for (Map.Entry 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); + } +} diff --git a/src/main/java/com/alibaba/excel/util/SheetUtils.java b/src/main/java/com/alibaba/excel/util/SheetUtils.java index 66a1c480..398fe5a4 100644 --- a/src/main/java/com/alibaba/excel/util/SheetUtils.java +++ b/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 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(); } diff --git a/update.md b/update.md index 09685283..8f2c4774 100644 --- a/update.md +++ b/update.md @@ -4,6 +4,7 @@ * 修复xls 用Map接收时多次接收会是同一个对象的bug * 修复浮点型数据导入到excel 会丢失精度的bug * 新增支持接收批注 +* 如果是`RuntimeException`则不再封装对象 # 2.1.4