From 6ed614612abedf717262c49a656a07ae2e391b87 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 2 Jan 2020 16:41:23 +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 --- .../excel/analysis/ExcelAnalyserImpl.java | 37 ++-- .../excel/analysis/ExcelReadExecutor.java | 7 +- .../analysis/v03/XlsListSheetListener.java | 39 ++-- .../excel/analysis/v03/XlsRecordHandler.java | 2 +- .../excel/analysis/v03/XlsSaxAnalyser.java | 198 ++++++------------ .../v03/handlers/BlankRecordHandler.java | 5 +- .../v03/handlers/BofRecordHandler.java | 33 +-- .../v03/handlers/BoolErrRecordHandler.java | 6 +- .../v03/handlers/BoundSheetRecordHandler.java | 4 +- ...rdHandler.java => DummyRecordHandler.java} | 15 +- .../v03/handlers/EofRecordHandler.java | 2 +- .../v03/handlers/FormulaRecordHandler.java | 18 +- .../v03/handlers/IndexRecordHandler.java | 2 +- .../v03/handlers/LabelRecordHandler.java | 12 +- ...andler.java => LabelSstRecordHandler.java} | 22 +- .../v03/handlers/NoteRecordHandler.java | 8 +- .../v03/handlers/NumberRecordHandler.java | 14 +- .../v03/handlers/ObjRecordHandler.java | 4 +- .../v03/handlers/RkRecordHandler.java | 5 +- .../v03/handlers/SstRecordHandler.java | 2 +- .../v03/handlers/StringRecordHandler.java | 10 +- .../v03/handlers/TextObjectRecordHandler.java | 10 +- .../excel/context/AnalysisContextImpl.java | 49 ++++- .../excel/context/DefaultXlsReadContext.java | 126 ----------- .../alibaba/excel/context/XlsReadContext.java | 95 --------- .../context/xls/DefaultXlsReadContext.java | 30 +++ .../excel/context/xls/XlsReadContext.java | 26 +++ .../context/xlsx/DefaultXlsxReadContext.java | 30 +++ .../excel/context/xlsx/XlsxReadContext.java | 28 +++ .../metadata/holder/ReadWorkbookHolder.java | 75 ++++--- .../holder/xls/XlsReadSheetHolder.java | 112 ++++++++++ .../holder/xls/XlsReadWorkbookHolder.java | 95 +++++++++ .../holder/xlsx/XlsxReadSheetHolder.java | 16 ++ .../holder/xlsx/XlsxReadWorkbookHolder.java | 52 +++++ .../com/alibaba/excel/util/SheetUtils.java | 2 +- 35 files changed, 670 insertions(+), 521 deletions(-) rename src/main/java/com/alibaba/excel/analysis/v03/handlers/{DummyRecordRecordHandler.java => DummyRecordHandler.java} (65%) rename src/main/java/com/alibaba/excel/analysis/v03/handlers/{LabelSSTRecordHandler.java => LabelSstRecordHandler.java} (50%) delete mode 100644 src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java delete mode 100644 src/main/java/com/alibaba/excel/context/XlsReadContext.java create mode 100644 src/main/java/com/alibaba/excel/context/xls/DefaultXlsReadContext.java create mode 100644 src/main/java/com/alibaba/excel/context/xls/XlsReadContext.java create mode 100644 src/main/java/com/alibaba/excel/context/xlsx/DefaultXlsxReadContext.java create mode 100644 src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java create mode 100644 src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 90cfad05..97ee3af4 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/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.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContextImpl; -import com.alibaba.excel.context.DefaultXlsReadContext; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.DefaultXlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; 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.util.CollectionUtils; import com.alibaba.excel.util.DateUtils; @@ -58,7 +60,6 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { private void choiceExcelExecutor(ReadWorkbook readWorkbook) throws Exception { ExcelTypeEnum excelType = ExcelTypeEnum.valueOf(readWorkbook.getFile(), readWorkbook.getInputStream(), readWorkbook.getExcelType()); - readWorkbook.setExcelType(excelType); switch (excelType) { case XLS: POIFSFileSystem poifsFileSystem; @@ -73,7 +74,7 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { try { decryptedStream = DocumentFactoryHelper .getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword()); - analysisContext = new AnalysisContextImpl(readWorkbook); + analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); return; } finally { @@ -86,15 +87,17 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { if (readWorkbook.getPassword() != null) { Biff8EncryptionKey.setCurrentUserPassword(readWorkbook.getPassword()); } - XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook); + XlsReadContext xlsReadContext = new DefaultXlsReadContext(readWorkbook, ExcelTypeEnum.XLS); + xlsReadContext.xlsReadWorkbookHolder().setPoifsFileSystem(poifsFileSystem); analysisContext = xlsReadContext; - excelReadExecutor = new XlsSaxAnalyser(xlsReadContext, poifsFileSystem); + excelReadExecutor = new XlsSaxAnalyser(xlsReadContext); break; case XLSX: - analysisContext = new AnalysisContextImpl(readWorkbook); + analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null); break; default: + break; } } @@ -104,19 +107,15 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { if (!readAll && CollectionUtils.isEmpty(readSheetList)) { throw new IllegalArgumentException("Specify at least one read sheet."); } + analysisContext.readWorkbookHolder().setParametersheetDataList(readSheetList); + analysisContext.readWorkbookHolder().setReadAll(readAll); try { - excelReadExecutor.execute(readSheetList, readAll); + excelReadExecutor.execute(); } catch (ExcelAnalysisStopException e) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Custom stop!"); } } - // The last sheet is read - if (excelReadExecutor instanceof XlsSaxAnalyser) { - if (analysisContext.readSheetHolder() != null) { - analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); - } - } } catch (RuntimeException e) { finish(); throw e; @@ -147,15 +146,17 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { throwable = t; } try { - if (readWorkbookHolder.getOpcPackage() != null) { - readWorkbookHolder.getOpcPackage().revert(); + if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder) + && ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage() != null) { + ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage().revert(); } } catch (Throwable t) { throwable = t; } try { - if (readWorkbookHolder.getPoifsFileSystem() != null) { - readWorkbookHolder.getPoifsFileSystem().close(); + if ((readWorkbookHolder instanceof XlsReadWorkbookHolder) + && ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem() != null) { + ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem().close(); } } catch (Throwable t) { throwable = t; diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java b/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java index 10ced98d..085caab8 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelReadExecutor.java @@ -20,11 +20,6 @@ public interface ExcelReadExecutor { /** * Read the sheet. - * - * @param readSheetList - * Which sheets you need to read. - * @param readAll - * The readSheetList parameter is ignored, and all sheets are read. */ - void execute(List readSheetList, Boolean readAll); + void execute(); } 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 906b3773..31d2db8c 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java @@ -1,8 +1,8 @@ package com.alibaba.excel.analysis.v03; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; 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.HSSFRequest; 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.poifs.filesystem.POIFSFileSystem; 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.read.metadata.ReadSheet; /** * 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 */ public class XlsListSheetListener implements HSSFListener { - private POIFSFileSystem poifsFileSystem; - private BofRecordHandler bofRecordHandler; + private XlsReadContext xlsReadContext; + private static final Map XLS_RECORD_HANDLER_MAP = new HashMap(); - 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) { - this.poifsFileSystem = poifsFileSystem; - sheetList = new ArrayList(); - bofRecordHandler = new BofRecordHandler(analysisContext, sheetList, false, false); - bofRecordHandler.init(); - bofRecordHandler.init(null, true); + public XlsListSheetListener(XlsReadContext xlsReadContext) { + this.xlsReadContext = xlsReadContext; } @Override 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 getSheetList() { + public void execute() { MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); HSSFListener formatListener = new FormatTrackingHSSFListener(listener); HSSFEventFactory factory = new HSSFEventFactory(); @@ -51,10 +55,9 @@ public class XlsListSheetListener implements HSSFListener { new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); request.addListenerForAllRecords(workbookBuildingListener); try { - factory.processWorkbookEvents(request, poifsFileSystem); + factory.processWorkbookEvents(request, xlsReadContext.xlsReadWorkbookHolder().getPoifsFileSystem()); } catch (IOException e) { throw new ExcelAnalysisException(e); } - return sheetList; } } 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 fca15b52..dea327f5 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsRecordHandler.java +++ b/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 com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Intercepts handle xls reads. 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 68b0c474..c90f6559 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java @@ -1,8 +1,6 @@ package com.alibaba.excel.analysis.v03; import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; 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.record.BOFRecord; 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.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.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.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.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.IndexRecordHandler; 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.NumberRecordHandler; +import com.alibaba.excel.analysis.v03.handlers.ObjRecordHandler; import com.alibaba.excel.analysis.v03.handlers.RkRecordHandler; 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.context.XlsReadContext; -import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.context.xls.XlsReadContext; 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.holder.ReadRowHolder; -import com.alibaba.excel.util.CollectionUtils; +import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder; /** * /** * A text extractor for Excel files. * @@ -61,169 +72,78 @@ 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 static final short DUMMY_RECORD_SID = -1; private XlsReadContext xlsReadContext; - private POIFSFileSystem poifsFileSystem; - private static final Map XLS_RECORD_HANDLER_MAP = new HashMap(32); static { 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()); + 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, POIFSFileSystem poifsFileSystem) { + public XlsSaxAnalyser(XlsReadContext xlsReadContext) { this.xlsReadContext = xlsReadContext; - this.poifsFileSystem = poifsFileSystem; - xlsReadContext.readWorkbookHolder().setPoifsFileSystem(poifsFileSystem); } @Override public List sheetList() { - if (xlsReadContext.readSheetDataList() == null) { - earlySheetDataList(); - + if (xlsReadContext.readWorkbookHolder().getActualSheetDataList() == null) { 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(); - } - - 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); + return xlsReadContext.readWorkbookHolder().getActualSheetDataList(); } @Override public void execute() { - this.readAll = readAll; - this.readSheetList = readSheetList; + XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder(); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this); - formatListener = new FormatTrackingHSSFListener(listener); - workbookBuildingListener = new EventWorkbookBuilder.SheetRecordCollectingListener(formatListener); - if (workbookBuildingListener != null && stubWorkbook == null) { - stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook(); - } - init(); + xlsReadWorkbookHolder.setFormatTrackingHSSFListener(new FormatTrackingHSSFListener(listener)); + EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener = + new EventWorkbookBuilder.SheetRecordCollectingListener( + xlsReadWorkbookHolder.getFormatTrackingHSSFListener()); + xlsReadWorkbookHolder.setHsffWorkbook(workbookBuildingListener.getStubHSSFWorkbook()); HSSFEventFactory factory = new HSSFEventFactory(); HSSFRequest request = new HSSFRequest(); - request.addListenerForAllRecords(formatListener); + request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener()); try { - factory.processWorkbookEvents(request, poifsFileSystem); + factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem()); } catch (IOException e) { throw new ExcelAnalysisException(e); } // Sometimes tables lack the end record of the last column - if (!xlsReadContext.cellMap().isEmpty()) { - endRow(); + if (!xlsReadContext.xlsReadSheetHolder().getCellMap().isEmpty()) { + // Forge a termination data + processRecord(new LastCellOfRowDummyRecord(xlsReadContext.xlsReadSheetHolder().getRowIndex() + 1, -1)); } } - private void init() { - recordHandlerMap = new HashMap(32); - - buildXlsRecordHandlers(); - - } - @Override public void processRecord(Record record) { - XlsRecordHandler handler = recordHandlerMap.get(record.getSid()); - if ((handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.readWorkbookHolder().getIgnoreRecord03()) { + XlsRecordHandler handler = XLS_RECORD_HANDLER_MAP.get(record.getSid()); + if (handler == null) { + return; + } + if ((handler instanceof IgnorableXlsRecordHandler) && xlsReadContext.xlsReadSheetHolder().getIgnoreRecord()) { // No need to read the current sheet return; } 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(); - 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); - } - } - } } 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 index 24d99058..372a44ca 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankRecordHandler.java +++ b/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 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; /** @@ -17,6 +17,7 @@ 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())); + xlsReadContext.xlsReadSheetHolder().getCellMap().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 5137d18a..59870bad 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 @@ -8,9 +8,11 @@ import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.Record; 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.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; /** @@ -26,43 +28,44 @@ public class BofRecordHandler implements XlsRecordHandler { if (br.getType() != BOFRecord.TYPE_WORKSHEET) { return; } + XlsReadWorkbookHolder xlsReadWorkbookHolder = xlsReadContext.xlsReadWorkbookHolder(); + XlsReadSheetHolder XlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder(); // Init read sheet Data - initReadSheetDataList(xlsReadContext); - Integer readSheetIndex = xlsReadContext.readSheetIndex(); + initReadSheetDataList(xlsReadWorkbookHolder); + Integer readSheetIndex = xlsReadWorkbookHolder.getReadSheetIndex(); if (readSheetIndex == null) { readSheetIndex = 0; - xlsReadContext.readSheetIndex(readSheetIndex); + xlsReadWorkbookHolder.setReadSheetIndex(readSheetIndex); } - - ReadSheet readSheet = xlsReadContext.readSheetDataList().get(readSheetIndex); + ReadSheet readSheet = xlsReadWorkbookHolder.getActualSheetDataList().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); + xlsReadContext.currentSheet(readSheet); + XlsReadSheetHolder.setIgnoreRecord(Boolean.FALSE); } else { - xlsReadContext.ignoreRecord(Boolean.TRUE); + XlsReadSheetHolder.setIgnoreRecord(Boolean.TRUE); } // Go read the next one - xlsReadContext.readSheetIndex(xlsReadContext.readSheetIndex() + 1); + xlsReadWorkbookHolder.setReadSheetIndex(xlsReadWorkbookHolder.getReadSheetIndex() + 1); } - private void initReadSheetDataList(XlsReadContext xlsReadContext) { - if (xlsReadContext.readSheetDataList() != null) { + private void initReadSheetDataList(XlsReadWorkbookHolder xlsReadWorkbookHolder) { + if (xlsReadWorkbookHolder.getActualSheetDataList() != null) { return; } BoundSheetRecord[] boundSheetRecords = - BoundSheetRecord.orderByBofPosition(xlsReadContext.boundSheetRecordList()); + BoundSheetRecord.orderByBofPosition(xlsReadWorkbookHolder.getBoundSheetRecordList()); 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); + xlsReadWorkbookHolder.setActualSheetDataList(readSheetDataList); // 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."); } } 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 index 27d521c2..60e72338 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoolErrRecordHandler.java +++ b/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 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.metadata.CellData; @@ -18,8 +18,8 @@ public class BoolErrRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record 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())); - xlsReadContext.tempRowType(RowTypeEnum.DATA); + xlsReadContext.xlsReadSheetHolder().setTempRowType(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 index 809d7fd3..96e7b33c 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java +++ b/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 com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Record handler @@ -16,6 +16,6 @@ public class BoundSheetRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { BoundSheetRecord bsr = (BoundSheetRecord)record; - xlsReadContext.boundSheetRecordList().add((BoundSheetRecord)record); + xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().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/DummyRecordHandler.java similarity index 65% rename from src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java rename to src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordHandler.java index 53d4d355..15a68dc9 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/DummyRecordRecordHandler.java +++ b/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 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.CellData; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; +import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder; /** * Record handler * * @author Dan Zheng */ -public class DummyRecordRecordHandler implements IgnorableXlsRecordHandler { +public class DummyRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { + XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder(); if (record instanceof LastCellOfRowDummyRecord) { // End of this row LastCellOfRowDummyRecord lcrdr = (LastCellOfRowDummyRecord)record; - xlsReadContext.rowIndex(lcrdr.getRow()); - xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadContext.tempRowType(), + xlsReadSheetHolder.setRowIndex(lcrdr.getRow()); + xlsReadContext.readRowHolder(new ReadRowHolder(lcrdr.getRow(), xlsReadSheetHolder.getTempRowType(), xlsReadContext.readSheetHolder().getGlobalConfiguration())); xlsReadContext.analysisEventProcessor().endRow(xlsReadContext); - xlsReadContext.cellMap(new LinkedHashMap()); + xlsReadSheetHolder.setCellMap(new LinkedHashMap()); } else if (record instanceof MissingCellDummyRecord) { 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())); } } } 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 index 9efe9885..4b1517b3 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/EofRecordHandler.java +++ b/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 com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Record handler 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 261b7e10..f04cdb77 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 @@ -1,6 +1,7 @@ package com.alibaba.excel.analysis.v03.handlers; import java.math.BigDecimal; +import java.util.Map; import org.apache.poi.hssf.model.HSSFFormulaParser; import org.apache.poi.hssf.record.FormulaRecord; @@ -10,8 +11,9 @@ 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.context.xls.XlsReadContext; import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.Cell; import com.alibaba.excel.metadata.CellData; /** @@ -26,13 +28,15 @@ public class FormulaRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { FormulaRecord frec = (FormulaRecord)record; + Map cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap(); 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()); + formulaValue = HSSFFormulaParser.toFormulaString(xlsReadContext.xlsReadWorkbookHolder().getHsffWorkbook(), + frec.getParsedExpression()); } catch (Exception e) { LOGGER.debug("Get formula value error.", e); } @@ -43,26 +47,26 @@ public class FormulaRecordHandler implements IgnorableXlsRecordHandler { // Formula result is a string // This is stored in the next record tempCellData.setType(CellDataTypeEnum.STRING); - xlsReadContext.tempCellData(tempCellData); + xlsReadContext.xlsReadSheetHolder().setTempCellData(tempCellData); break; case NUMERIC: tempCellData.setType(CellDataTypeEnum.NUMBER); tempCellData.setNumberValue(BigDecimal.valueOf(frec.getValue())); - xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + cellMap.put((int)frec.getColumn(), tempCellData); break; case ERROR: tempCellData.setType(CellDataTypeEnum.ERROR); tempCellData.setStringValue(ERROR); - xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + cellMap.put((int)frec.getColumn(), tempCellData); break; case BOOLEAN: tempCellData.setType(CellDataTypeEnum.BOOLEAN); tempCellData.setBooleanValue(frec.getCachedBooleanValue()); - xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + cellMap.put((int)frec.getColumn(), tempCellData); break; default: tempCellData.setType(CellDataTypeEnum.EMPTY); - xlsReadContext.cellMap().put((int)frec.getColumn(), tempCellData); + cellMap.put((int)frec.getColumn(), tempCellData); break; } } 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 08a5a569..de21da89 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 @@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.IndexRecord; import org.apache.poi.hssf.record.Record; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Record handler 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 93259e61..1335ada3 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 @@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.LabelRecord; import org.apache.poi.hssf.record.Record; 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.metadata.CellData; @@ -17,8 +17,12 @@ public class LabelRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { LabelRecord lrec = (LabelRecord)record; - xlsReadContext.cellMap().put((int)lrec.getColumn(), - CellData.newInstance(lrec.getValue(), lrec.getRow(), (int)lrec.getColumn())); - xlsReadContext.tempRowType(RowTypeEnum.DATA); + String data = lrec.getValue(); + if (data != null && xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) { + data = data.trim(); + } + xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)lrec.getColumn(), + CellData.newInstance(data, lrec.getRow(), (int)lrec.getColumn())); + xlsReadContext.xlsReadSheetHolder().setTempRowType(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 similarity index 50% rename from src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java rename to src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSstRecordHandler.java index 5e08ff87..7979241e 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSSTRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/LabelSstRecordHandler.java @@ -1,12 +1,15 @@ package com.alibaba.excel.analysis.v03.handlers; +import java.util.Map; + 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.context.xls.XlsReadContext; import com.alibaba.excel.enums.RowTypeEnum; +import com.alibaba.excel.metadata.Cell; import com.alibaba.excel.metadata.CellData; /** @@ -14,25 +17,26 @@ import com.alibaba.excel.metadata.CellData; * * @author Dan Zheng */ -public class LabelSSTRecordHandler implements IgnorableXlsRecordHandler { +public class LabelSstRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { LabelSSTRecord lsrec = (LabelSSTRecord)record; ReadCache readCache = xlsReadContext.readWorkbookHolder().getReadCache(); + Map cellMap = xlsReadContext.xlsReadSheetHolder().getCellMap(); if (readCache == null) { - xlsReadContext.cellMap().put((int)lsrec.getColumn(), - CellData.newEmptyInstance(lsrec.getRow(), (int)lsrec.getColumn())); + 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())); + 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); + if (xlsReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) { + data = data.trim(); + } + cellMap.put((int)lsrec.getColumn(), CellData.newInstance(data, lsrec.getRow(), (int)lsrec.getColumn())); + xlsReadContext.xlsReadSheetHolder().setTempRowType(RowTypeEnum.DATA); } } 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 255eb988..8c2993dc 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 @@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.NoteRecord; import org.apache.poi.hssf.record.Record; 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.metadata.CellExtra; @@ -18,12 +18,12 @@ public class NoteRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record 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.setRowIndex(nr.getRow()); cellExtra.setRowIndex(nr.getColumn()); cellExtra.setNote(note); - xlsReadContext.cellMap().put(nr.getColumn(), cellExtra); - xlsReadContext.tempRowType(RowTypeEnum.EXTRA); + xlsReadContext.xlsReadSheetHolder().getCellMap().put(nr.getColumn(), cellExtra); + xlsReadContext.xlsReadSheetHolder().setTempRowType(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 381bacd4..5e2fbca4 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 @@ -7,7 +7,7 @@ import org.apache.poi.hssf.record.Record; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; 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.metadata.CellData; @@ -22,12 +22,12 @@ public class NumberRecordHandler implements IgnorableXlsRecordHandler { 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); + Integer dataFormat = xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().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); + cellData.setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, + xlsReadContext.xlsReadWorkbookHolder().getFormatTrackingHSSFListener().getFormatString(nr), + xlsReadContext.readSheetHolder().getGlobalConfiguration().getLocale())); + xlsReadContext.xlsReadSheetHolder().getCellMap().put((int)nr.getColumn(), cellData); + xlsReadContext.xlsReadSheetHolder().setTempRowType(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 index 5144a73f..e8a0cc1b 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/ObjRecordHandler.java +++ b/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 com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Record handler @@ -21,7 +21,7 @@ public class ObjRecordHandler implements IgnorableXlsRecordHandler { if (subRecord instanceof CommonObjectDataSubRecord) { CommonObjectDataSubRecord codsr = (CommonObjectDataSubRecord)subRecord; if (CommonObjectDataSubRecord.OBJECT_TYPE_COMMENT == codsr.getObjectType()) { - xlsReadContext.tempObjectIndex(codsr.getObjectId()); + xlsReadContext.xlsReadSheetHolder().setTempObjectIndex(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 cc902ef7..22ba166b 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 @@ -4,7 +4,7 @@ import org.apache.poi.hssf.record.RKRecord; import org.apache.poi.hssf.record.Record; 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; /** @@ -17,6 +17,7 @@ public class RkRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record 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())); } } 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 7f0f01d7..c11ff4c0 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 @@ -5,7 +5,7 @@ import org.apache.poi.hssf.record.SSTRecord; import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; import com.alibaba.excel.cache.XlsCache; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; /** * Record handler 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 index 63e69536..e4c40ecf 100644 --- a/src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v03/handlers/StringRecordHandler.java @@ -6,8 +6,9 @@ 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.context.xls.XlsReadContext; import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder; /** * Record handler @@ -21,13 +22,14 @@ public class StringRecordHandler implements IgnorableXlsRecordHandler { public void processRecord(XlsReadContext xlsReadContext, Record record) { // String for formula StringRecord srec = (StringRecord)record; - CellData tempCellData = xlsReadContext.tempCellData(); + XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder(); + CellData tempCellData = xlsReadSheetHolder.getTempCellData(); 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); + xlsReadSheetHolder.getCellMap().put(tempCellData.getColumnIndex(), tempCellData); + xlsReadSheetHolder.setTempCellData(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 764b69e6..1ee3543b 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 @@ -6,7 +6,8 @@ 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.context.xls.XlsReadContext; +import com.alibaba.excel.read.metadata.holder.xls.XlsReadSheetHolder; /** * Record handler @@ -19,12 +20,13 @@ public class TextObjectRecordHandler implements IgnorableXlsRecordHandler { @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { TextObjectRecord tor = (TextObjectRecord)record; - Integer tempObjectIndex = xlsReadContext.tempObjectIndex(); + XlsReadSheetHolder xlsReadSheetHolder = xlsReadContext.xlsReadSheetHolder(); + Integer tempObjectIndex = xlsReadSheetHolder.getTempObjectIndex(); if (tempObjectIndex == null) { LOGGER.debug("tempObjectIndex is null."); return; } - xlsReadContext.objectCacheMap().put(tempObjectIndex, tor.getStr().getString()); - xlsReadContext.tempObjectIndex(null); + xlsReadSheetHolder.getObjectCacheMap().put(tempObjectIndex, tor.getStr().getString()); + xlsReadSheetHolder.setTempObjectIndex(null); } } diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 9f03bcfa..8172b655 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -1,6 +1,7 @@ package com.alibaba.excel.context; import java.io.InputStream; +import java.util.List; import org.slf4j.Logger; 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.ReadSheetHolder; 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.DefalutAnalysisEventProcessor; import com.alibaba.excel.support.ExcelTypeEnum; @@ -44,11 +49,20 @@ public class AnalysisContextImpl implements AnalysisContext { */ private AnalysisEventProcessor analysisEventProcessor; - public AnalysisContextImpl(ReadWorkbook readWorkbook) { + public AnalysisContextImpl(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) { if (readWorkbook == 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; analysisEventProcessor = new DefalutAnalysisEventProcessor(); if (LOGGER.isDebugEnabled()) { @@ -58,7 +72,16 @@ public class AnalysisContextImpl implements AnalysisContext { @Override 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; if (readWorkbookHolder.getHasReadSheet().contains(readSheetHolder.getSheetNo())) { throw new ExcelAnalysisException("Cannot read sheet repeatedly."); @@ -104,6 +127,26 @@ public class AnalysisContextImpl implements AnalysisContext { return analysisEventProcessor; } + @Override + public List readSheetList() { + return null; + } + + @Override + public void readSheetList(List readSheetList) { + + } + + @Override + public Boolean readAll() { + return null; + } + + @Override + public void readAll(Boolean readAll) { + + } + @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 deleted file mode 100644 index 94878591..00000000 --- a/src/main/java/com/alibaba/excel/context/DefaultXlsReadContext.java +++ /dev/null @@ -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 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 deleted file mode 100644 index 7af83cbb..00000000 --- a/src/main/java/com/alibaba/excel/context/XlsReadContext.java +++ /dev/null @@ -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 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/context/xls/DefaultXlsReadContext.java b/src/main/java/com/alibaba/excel/context/xls/DefaultXlsReadContext.java new file mode 100644 index 00000000..698bf6a4 --- /dev/null +++ b/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(); + } +} diff --git a/src/main/java/com/alibaba/excel/context/xls/XlsReadContext.java b/src/main/java/com/alibaba/excel/context/xls/XlsReadContext.java new file mode 100644 index 00000000..4d1bcdb2 --- /dev/null +++ b/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(); +} diff --git a/src/main/java/com/alibaba/excel/context/xlsx/DefaultXlsxReadContext.java b/src/main/java/com/alibaba/excel/context/xlsx/DefaultXlsxReadContext.java new file mode 100644 index 00000000..5c9d638d --- /dev/null +++ b/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(); + } +} diff --git a/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java b/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java new file mode 100644 index 00000000..924cafea --- /dev/null +++ b/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(); +} 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 b97395ec..1c1b2420 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 @@ -4,6 +4,7 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.InputStream; import java.util.HashSet; +import java.util.List; import java.util.Set; 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.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelAnalysisException; +import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.support.ExcelTypeEnum; @@ -86,23 +88,25 @@ public class ReadWorkbookHolder extends AbstractReadHolder { * Whether the encryption */ private String password; - /** - * SAXParserFactory used when reading xlsx. - *

- * The default will automatically find. - *

- * 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. * * @see ExtraReadEnum */ private Set extraReadSet; + /** + * Actual sheet data + */ + private List actualSheetDataList; + /** + * Parameter sheet data + */ + private List 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 * 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 */ private Set hasReadSheet; - /** - * Package - */ - private OPCPackage opcPackage; - /** - * File System - */ - private POIFSFileSystem poifsFileSystem; public ReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook, null, readWorkbook.getConvertAllFiled()); @@ -184,7 +180,6 @@ public class ReadWorkbookHolder extends AbstractReadHolder { } else { this.defaultReturnMap = readWorkbook.getDefaultReturnMap(); } - this.xlsxSAXParserFactoryName = readWorkbook.getXlsxSAXParserFactoryName(); if (readWorkbook.getExtraReadSet() == null) { this.extraReadSet = new HashSet(); } else { @@ -306,44 +301,44 @@ public class ReadWorkbookHolder extends AbstractReadHolder { this.defaultReturnMap = defaultReturnMap; } - public OPCPackage getOpcPackage() { - return opcPackage; + public String getPassword() { + return password; } - public void setOpcPackage(OPCPackage opcPackage) { - this.opcPackage = opcPackage; + public void setPassword(String password) { + this.password = password; } - public POIFSFileSystem getPoifsFileSystem() { - return poifsFileSystem; + public Set getExtraReadSet() { + return extraReadSet; } - public void setPoifsFileSystem(POIFSFileSystem poifsFileSystem) { - this.poifsFileSystem = poifsFileSystem; + public void setExtraReadSet(Set extraReadSet) { + this.extraReadSet = extraReadSet; } - public String getPassword() { - return password; + public List getActualSheetDataList() { + return actualSheetDataList; } - public void setPassword(String password) { - this.password = password; + public void setActualSheetDataList(List actualSheetDataList) { + this.actualSheetDataList = actualSheetDataList; } - public String getXlsxSAXParserFactoryName() { - return xlsxSAXParserFactoryName; + public List getParametersheetDataList() { + return parametersheetDataList; } - public void setXlsxSAXParserFactoryName(String xlsxSAXParserFactoryName) { - this.xlsxSAXParserFactoryName = xlsxSAXParserFactoryName; + public void setParametersheetDataList(List parametersheetDataList) { + this.parametersheetDataList = parametersheetDataList; } - public Set getExtraReadSet() { - return extraReadSet; + public Boolean getReadAll() { + return readAll; } - public void setExtraReadSet(Set extraReadSet) { - this.extraReadSet = extraReadSet; + public void setReadAll(Boolean readAll) { + this.readAll = readAll; } @Override diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadSheetHolder.java new file mode 100644 index 00000000..a77a8d38 --- /dev/null +++ b/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 ReadRowHolder. + */ + private RowTypeEnum tempRowType; + /** + * Data storage of the current row. + */ + private Map 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 objectCacheMap; + + public XlsReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { + super(readSheet, readWorkbookHolder); + cellMap = new LinkedHashMap(); + ignoreRecord = Boolean.FALSE; + tempRowType = RowTypeEnum.EMPTY; + objectCacheMap = new HashMap(16); + } + + public RowTypeEnum getTempRowType() { + return tempRowType; + } + + public void setTempRowType(RowTypeEnum tempRowType) { + this.tempRowType = tempRowType; + } + + public Map getCellMap() { + return cellMap; + } + + public void setCellMap(Map 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 getObjectCacheMap() { + return objectCacheMap; + } + + public void setObjectCacheMap(Map objectCacheMap) { + this.objectCacheMap = objectCacheMap; + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xls/XlsReadWorkbookHolder.java new file mode 100644 index 00000000..c4b698cb --- /dev/null +++ b/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 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 getBoundSheetRecordList() { + return boundSheetRecordList; + } + + public void setBoundSheetRecordList(List 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; + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java new file mode 100644 index 00000000..b7439c39 --- /dev/null +++ b/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); + } +} diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java new file mode 100644 index 00000000..34ec6ad1 --- /dev/null +++ b/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. + *

+ * The default will automatically find. + *

+ * 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; + } +} diff --git a/src/main/java/com/alibaba/excel/util/SheetUtils.java b/src/main/java/com/alibaba/excel/util/SheetUtils.java index 398fe5a4..492789c3 100644 --- a/src/main/java/com/alibaba/excel/util/SheetUtils.java +++ b/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.LoggerFactory; -import com.alibaba.excel.context.XlsReadContext; +import com.alibaba.excel.context.xls.XlsReadContext; import com.alibaba.excel.read.metadata.ReadSheet; /**