From 13ca08e1d8601e23a535ad058e7015c903905cbc Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Sat, 14 Mar 2020 22:24:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=94=AF=E6=8C=81=E6=89=B9?= =?UTF-8?q?=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/analysis/ExcelAnalyserImpl.java | 21 ++-- .../excel/analysis/v07/XlsxCellHandler.java | 45 --------- .../analysis/v07/XlsxHandlerFactory.java | 1 + .../excel/analysis/v07/XlsxRowHandler.java | 81 --------------- .../excel/analysis/v07/XlsxSaxAnalyser.java | 98 ++++++++----------- .../v07/handlers/CountRowCellHandler.java | 26 +---- .../v07/handlers/DefaultCellHandler.java | 17 ---- .../handlers/ProcessResultCellHandler.java | 39 ++------ .../v07/handlers/XlsxCellHandler.java | 29 ++++++ .../sax}/SharedStringsTableHandler.java | 2 +- .../v07/handlers/sax/XlsxRowHandler.java | 58 +++++++++++ .../excel/context/AnalysisContextImpl.java | 4 +- .../excel/context/xlsx/XlsxReadContext.java | 2 - .../holder/xlsx/XlsxReadSheetHolder.java | 3 + .../holder/xlsx/XlsxReadWorkbookHolder.java | 13 +++ ...ava => DefaultAnalysisEventProcessor.java} | 12 ++- .../com/alibaba/excel/util/SheetUtils.java | 18 ++-- 17 files changed, 186 insertions(+), 283 deletions(-) delete mode 100644 src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java delete mode 100644 src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java create mode 100644 src/main/java/com/alibaba/excel/analysis/v07/handlers/XlsxCellHandler.java rename src/main/java/com/alibaba/excel/analysis/v07/{ => handlers/sax}/SharedStringsTableHandler.java (97%) create mode 100644 src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java rename src/main/java/com/alibaba/excel/read/processor/{DefalutAnalysisEventProcessor.java => DefaultAnalysisEventProcessor.java} (96%) diff --git a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 97ee3af..f8d81fa 100644 --- a/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -14,9 +14,10 @@ import org.slf4j.LoggerFactory; 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.xls.DefaultXlsReadContext; import com.alibaba.excel.context.xls.XlsReadContext; +import com.alibaba.excel.context.xlsx.DefaultXlsxReadContext; +import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.read.metadata.ReadSheet; @@ -74,8 +75,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { try { decryptedStream = DocumentFactoryHelper .getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword()); - analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); - excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); + XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX); + analysisContext = xlsxReadContext; + excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, decryptedStream); return; } finally { IOUtils.closeQuietly(decryptedStream); @@ -93,8 +95,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { excelReadExecutor = new XlsSaxAnalyser(xlsReadContext); break; case XLSX: - analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); - excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null); + XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX); + analysisContext = xlsxReadContext; + excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, null); break; default: break; @@ -147,16 +150,16 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } try { if ((readWorkbookHolder instanceof XlsxReadWorkbookHolder) - && ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage() != null) { - ((XlsxReadWorkbookHolder)readWorkbookHolder).getOpcPackage().revert(); + && ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage() != null) { + ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage().revert(); } } catch (Throwable t) { throwable = t; } try { if ((readWorkbookHolder instanceof XlsReadWorkbookHolder) - && ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem() != null) { - ((XlsReadWorkbookHolder)readWorkbookHolder).getPoifsFileSystem().close(); + && ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem() != null) { + ((XlsReadWorkbookHolder) readWorkbookHolder).getPoifsFileSystem().close(); } } catch (Throwable t) { throwable = t; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java deleted file mode 100644 index 3068725..0000000 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.alibaba.excel.analysis.v07; - -import org.xml.sax.Attributes; - -/** - * Cell handler - * - * @author Dan Zheng - */ -public interface XlsxCellHandler { - /** - * Which tags are supported - * - * @param name - * Tag name - * @return Support parsing or not - */ - boolean support(String name); - - /** - * Start handle - * - * @param name - * Tag name - * @param attributes - * Tag attributes - */ - void startHandle(String name, Attributes attributes); - - /** - * End handle - * - * @param name - * Tag name - */ - void endHandle(String name); - - /** - * Set the comment of the cell - * - * @param comment - * cell comment - */ - void handleComments(String comment); -} diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java index 82f7d2c..1785036 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java @@ -8,6 +8,7 @@ import org.apache.poi.xssf.model.StylesTable; import com.alibaba.excel.analysis.v07.handlers.CountRowCellHandler; import com.alibaba.excel.analysis.v07.handlers.DefaultCellHandler; import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler; +import com.alibaba.excel.analysis.v07.handlers.XlsxCellHandler; import com.alibaba.excel.context.AnalysisContext; /** diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java deleted file mode 100644 index bc433cf..0000000 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.alibaba.excel.analysis.v07; - -import java.util.List; - -import org.apache.poi.ss.util.CellAddress; -import org.apache.poi.xssf.model.CommentsTable; -import org.apache.poi.xssf.model.StylesTable; -import org.apache.poi.xssf.usermodel.XSSFComment; -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; - -/** - * - * @author jipengfei - */ -public class XlsxRowHandler extends DefaultHandler { - - private List cellHandlers; - private XlsxRowResultHolder rowResultHolder; - private CommentsTable commentsTable; - - public XlsxRowHandler(AnalysisContext analysisContext, StylesTable stylesTable, CommentsTable commentsTable) { - this(analysisContext, stylesTable); - this.commentsTable = commentsTable; - } - - public XlsxRowHandler(AnalysisContext analysisContext, StylesTable stylesTable) { - this.cellHandlers = XlsxHandlerFactory.buildCellHandlers(analysisContext, stylesTable); - for (XlsxCellHandler cellHandler : cellHandlers) { - if (cellHandler instanceof XlsxRowResultHolder) { - this.rowResultHolder = (XlsxRowResultHolder)cellHandler; - break; - } - } - } - - @Override - public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { - for (XlsxCellHandler cellHandler : cellHandlers) { - if (cellHandler.support(name)) { - cellHandler.startHandle(name, attributes); - handleComment(cellHandler, attributes.getValue(ExcelXmlConstants.POSITION)); - } - } - } - - @Override - public void endElement(String uri, String localName, String name) throws SAXException { - for (XlsxCellHandler cellHandler : cellHandlers) { - if (cellHandler.support(name)) { - cellHandler.endHandle(name); - } - } - } - - @Override - public void characters(char[] ch, int start, int length) throws SAXException { - if (rowResultHolder != null) { - rowResultHolder.appendCurrentCellValue(ch, start, length); - } - } - - private void handleComment(XlsxCellHandler cellHandler, String address) { - if (StringUtils.isEmpty(address) || null == commentsTable) { - return; - } - XSSFComment xssfComment = commentsTable.getCellComments().get(new CellAddress(address)); - if (null == xssfComment) { - return; - } - String comments = xssfComment.getString().toString(); - if (!StringUtils.isEmpty(comments)) { - cellHandler.handleComments(comments); - } - } -} diff --git a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index 374dc45..205b648 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -16,8 +16,6 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.xssf.eventusermodel.XSSFReader; -import org.apache.poi.xssf.model.CommentsTable; -import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFRelation; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; @@ -27,45 +25,34 @@ import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import com.alibaba.excel.analysis.ExcelReadExecutor; +import com.alibaba.excel.analysis.v07.handlers.sax.SharedStringsTableHandler; +import com.alibaba.excel.analysis.v07.handlers.sax.XlsxRowHandler; import com.alibaba.excel.cache.ReadCache; -import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.context.xlsx.XlsxReadContext; import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.read.metadata.ReadSheet; -import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; +import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.SheetUtils; import com.alibaba.excel.util.StringUtils; /** - * * @author jipengfei */ public class XlsxSaxAnalyser implements ExcelReadExecutor { - private AnalysisContext analysisContext; + private XlsxReadContext xlsxReadContext; private List sheetList; private Map sheetMap; - /** - * excel comments - * key: sheetNo - * value: CommentsTanle - */ - private Map commentsTableMap; - - /** - * Current style information - */ - private StylesTable stylesTable; - - public XlsxSaxAnalyser(AnalysisContext analysisContext, InputStream decryptedStream) throws Exception { - this.analysisContext = analysisContext; + public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception { + this.xlsxReadContext = xlsxReadContext; // Initialize cache - ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); + XlsxReadWorkbookHolder xlsxReadWorkbookHolder = xlsxReadContext.xlsxReadWorkbookHolder(); - OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); - readWorkbookHolder.setOpcPackage(pkg); + OPCPackage pkg = readOpcPackage(xlsxReadWorkbookHolder, decryptedStream); + xlsxReadWorkbookHolder.setOpcPackage(pkg); ArrayList packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()); @@ -73,20 +60,19 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { PackagePart sharedStringsTablePackagePart = packageParts.get(0); // Specify default cache - defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart); + defaultReadCache(xlsxReadWorkbookHolder, sharedStringsTablePackagePart); // Analysis sharedStringsTable.xml - analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); + analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), xlsxReadWorkbookHolder); } XSSFReader xssfReader = new XSSFReader(pkg); - analysisUse1904WindowDate(xssfReader, readWorkbookHolder); + analysisUse1904WindowDate(xssfReader, xlsxReadWorkbookHolder); - stylesTable = xssfReader.getStylesTable(); + xlsxReadWorkbookHolder.setStylesTable(xssfReader.getStylesTable()); sheetList = new ArrayList(); sheetMap = new HashMap(); - commentsTableMap = new HashMap(); - XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator)xssfReader.getSheetsData(); + XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); int index = 0; if (!ite.hasNext()) { throw new ExcelAnalysisException("Can not find any sheet!"); @@ -95,23 +81,19 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { InputStream inputStream = ite.next(); sheetList.add(new ReadSheet(index, ite.getSheetName())); sheetMap.put(index, inputStream); - CommentsTable commentsTable = ite.getSheetComments(); - if (null != commentsTable) { - commentsTableMap.put(index, commentsTable); - } index++; } } - private void defaultReadCache(ReadWorkbookHolder readWorkbookHolder, PackagePart sharedStringsTablePackagePart) { - ReadCache readCache = readWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart); - readWorkbookHolder.setReadCache(readCache); - readCache.init(analysisContext); + private void defaultReadCache(XlsxReadWorkbookHolder xlsxReadWorkbookHolder, PackagePart sharedStringsTablePackagePart) { + ReadCache readCache = xlsxReadWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart); + xlsxReadWorkbookHolder.setReadCache(readCache); + readCache.init(xlsxReadContext); } - private void analysisUse1904WindowDate(XSSFReader xssfReader, ReadWorkbookHolder readWorkbookHolder) + private void analysisUse1904WindowDate(XSSFReader xssfReader, XlsxReadWorkbookHolder xlsxReadWorkbookHolder) throws Exception { - if (readWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { + if (xlsxReadWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { return; } InputStream workbookXml = xssfReader.getWorkbookData(); @@ -119,38 +101,38 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { CTWorkbook wb = ctWorkbook.getWorkbook(); CTWorkbookPr prefix = wb.getWorkbookPr(); if (prefix != null && prefix.getDate1904()) { - readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); + xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); } else { - readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); + xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); } } private void analysisSharedStringsTable(InputStream sharedStringsTableInputStream, - ReadWorkbookHolder readWorkbookHolder) throws Exception { - ContentHandler handler = new SharedStringsTableHandler(readWorkbookHolder.getReadCache()); + XlsxReadWorkbookHolder xlsxReadWorkbookHolder) throws Exception { + ContentHandler handler = new SharedStringsTableHandler(xlsxReadWorkbookHolder.getReadCache()); parseXmlSource(sharedStringsTableInputStream, handler); - readWorkbookHolder.getReadCache().putFinished(); + xlsxReadWorkbookHolder.getReadCache().putFinished(); } - private OPCPackage readOpcPackage(ReadWorkbookHolder readWorkbookHolder, InputStream decryptedStream) + private OPCPackage readOpcPackage(XlsxReadWorkbookHolder xlsxReadWorkbookHolder, InputStream decryptedStream) throws Exception { - if (decryptedStream == null && readWorkbookHolder.getFile() != null) { - return OPCPackage.open(readWorkbookHolder.getFile()); + if (decryptedStream == null && xlsxReadWorkbookHolder.getFile() != null) { + return OPCPackage.open(xlsxReadWorkbookHolder.getFile()); } - if (readWorkbookHolder.getMandatoryUseInputStream()) { + if (xlsxReadWorkbookHolder.getMandatoryUseInputStream()) { if (decryptedStream != null) { return OPCPackage.open(decryptedStream); } else { - return OPCPackage.open(readWorkbookHolder.getInputStream()); + return OPCPackage.open(xlsxReadWorkbookHolder.getInputStream()); } } File readTempFile = FileUtils.createCacheTmpFile(); - readWorkbookHolder.setTempFile(readTempFile); + xlsxReadWorkbookHolder.setTempFile(readTempFile); File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); if (decryptedStream != null) { FileUtils.writeToFile(tempFile, decryptedStream); } else { - FileUtils.writeToFile(tempFile, readWorkbookHolder.getInputStream()); + FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream()); } return OPCPackage.open(tempFile, PackageAccess.READ); } @@ -164,7 +146,7 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { InputSource inputSource = new InputSource(inputStream); try { SAXParserFactory saxFactory; - String xlsxSAXParserFactoryName = analysisContext.readWorkbookHolder().getXlsxSAXParserFactoryName(); + String xlsxSAXParserFactoryName = xlsxReadContext.xlsxReadWorkbookHolder().getSaxParserFactoryName(); if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) { saxFactory = SAXParserFactory.newInstance(); } else { @@ -194,16 +176,14 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor { } @Override - public void execute(List readSheetList, Boolean readAll) { + public void execute() { for (ReadSheet readSheet : sheetList) { - readSheet = SheetUtils.match(readSheet, readSheetList, readAll, - analysisContext.readWorkbookHolder().getGlobalConfiguration()); + readSheet = SheetUtils.match(readSheet, xlsxReadContext); if (readSheet != null) { - analysisContext.currentSheet(readSheet); - Integer sheetNo = readSheet.getSheetNo(); - parseXmlSource(sheetMap.get(sheetNo), new XlsxRowHandler(analysisContext, stylesTable, commentsTableMap.get(sheetNo))); + xlsxReadContext.currentSheet(readSheet); + parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext)); // The last sheet is read - analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); + xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext); } } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java index c838aec..dbc0a7e 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java @@ -1,12 +1,10 @@ package com.alibaba.excel.analysis.v07.handlers; -import static com.alibaba.excel.constant.ExcelXmlConstants.DIMENSION; import static com.alibaba.excel.constant.ExcelXmlConstants.DIMENSION_REF; import org.xml.sax.Attributes; -import com.alibaba.excel.analysis.v07.XlsxCellHandler; -import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.context.xlsx.XlsxReadContext; /** * Cell Handler @@ -15,33 +13,17 @@ import com.alibaba.excel.context.AnalysisContext; */ public class CountRowCellHandler implements XlsxCellHandler { - private final AnalysisContext analysisContext; - - public CountRowCellHandler(AnalysisContext analysisContext) { - this.analysisContext = analysisContext; - } - - @Override - public boolean support(String name) { - return DIMENSION.equals(name); - } @Override - public void startHandle(String name, Attributes attributes) { + public void startHandle(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { String d = attributes.getValue(DIMENSION_REF); String totalStr = d.substring(d.indexOf(":") + 1, d.length()); String c = totalStr.toUpperCase().replaceAll("[A-Z]", ""); - analysisContext.readSheetHolder().setApproximateTotalRowNumber(Integer.parseInt(c)); + xlsxReadContext.readSheetHolder().setApproximateTotalRowNumber(Integer.parseInt(c)); } @Override - public void endHandle(String name) { - - } - - @Override - public void handleComments(String comment) { - + public void endHandle(XlsxReadContext xlsxReadContext, String name) { } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java index 3a489e1..baa3990 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java @@ -13,12 +13,10 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; -import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.xml.sax.Attributes; -import com.alibaba.excel.analysis.v07.XlsxCellHandler; import com.alibaba.excel.analysis.v07.XlsxRowResultHolder; import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.constant.ExcelXmlConstants; @@ -42,16 +40,6 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder private StringBuilder dataStringBuilder; private StringBuilder formulaStringBuilder; - /** - * Current style information - */ - private StylesTable stylesTable; - - public DefaultCellHandler(AnalysisContext analysisContext, StylesTable stylesTable) { - this.analysisContext = analysisContext; - this.stylesTable = stylesTable; - } - @Override public void clearResult() { curRowContent = new LinkedHashMap(); @@ -157,11 +145,6 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder } } - @Override - public void handleComments(String comment) { - // analysisContext.readRowHolder().addComments(curCol, comment); - } - @Override public void appendCurrentCellValue(char[] ch, int start, int length) { String currentTag = currentTagDeque.peek(); diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java index 32b75ed..51a7123 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java @@ -1,14 +1,10 @@ package com.alibaba.excel.analysis.v07.handlers; -import static com.alibaba.excel.constant.ExcelXmlConstants.ROW_TAG; - import org.xml.sax.Attributes; -import com.alibaba.excel.analysis.v07.XlsxCellHandler; -import com.alibaba.excel.analysis.v07.XlsxRowResultHolder; import com.alibaba.excel.constant.ExcelXmlConstants; -import com.alibaba.excel.context.AnalysisContext; -import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; +import com.alibaba.excel.context.xlsx.XlsxReadContext; +import com.alibaba.excel.enums.RowTypeEnum; import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.util.PositionUtils; @@ -18,36 +14,17 @@ import com.alibaba.excel.util.PositionUtils; * @author jipengfei */ public class ProcessResultCellHandler implements XlsxCellHandler { - private AnalysisContext analysisContext; - private XlsxRowResultHolder rowResultHandler; - - public ProcessResultCellHandler(AnalysisContext analysisContext, XlsxRowResultHolder rowResultHandler) { - this.analysisContext = analysisContext; - this.rowResultHandler = rowResultHandler; - } - - @Override - public boolean support(String name) { - return ROW_TAG.equals(name); - } @Override - public void startHandle(String name, Attributes attributes) { - analysisContext.readRowHolder( - new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), - analysisContext.readSheetHolder().getGlobalConfiguration())); + public void startHandle(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { + xlsxReadContext.readRowHolder( + new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), RowTypeEnum.DATA, + xlsxReadContext.readSheetHolder().getGlobalConfiguration())); } @Override - public void endHandle(String name) { - analysisContext.readSheetHolder() - .notifyEndOneRow(new EachRowAnalysisFinishEvent(rowResultHandler.getCurRowContent()), analysisContext); - rowResultHandler.clearResult(); - } - - @Override - public void handleComments(String comment) { - + public void endHandle(XlsxReadContext xlsxReadContext, String name) { + xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); } } diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/XlsxCellHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/XlsxCellHandler.java new file mode 100644 index 0000000..3ec2fd8 --- /dev/null +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/XlsxCellHandler.java @@ -0,0 +1,29 @@ +package com.alibaba.excel.analysis.v07.handlers; + +import org.xml.sax.Attributes; + +import com.alibaba.excel.context.xlsx.XlsxReadContext; + +/** + * Cell handler + * + * @author Dan Zheng + */ +public interface XlsxCellHandler { + /** + * Start handle + * + * @param xlsxReadContext xlsxReadContext + * @param name Tag name + * @param attributes Tag attributes + */ + void startHandle(XlsxReadContext xlsxReadContext, String name, Attributes attributes); + + /** + * End handle + * + * @param xlsxReadContext xlsxReadContext + * @param name Tag name + */ + void endHandle(XlsxReadContext xlsxReadContext, String name); +} diff --git a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java similarity index 97% rename from src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java rename to src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java index 5e001ed..ea6ffc5 100644 --- a/src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java @@ -1,4 +1,4 @@ -package com.alibaba.excel.analysis.v07; +package com.alibaba.excel.analysis.v07.handlers.sax; import org.xml.sax.Attributes; import org.xml.sax.helpers.DefaultHandler; diff --git a/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java b/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java new file mode 100644 index 0000000..90e760a --- /dev/null +++ b/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java @@ -0,0 +1,58 @@ +package com.alibaba.excel.analysis.v07.handlers.sax; + +import java.util.HashMap; +import java.util.Map; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import com.alibaba.excel.analysis.v07.handlers.CountRowCellHandler; +import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler; +import com.alibaba.excel.analysis.v07.handlers.XlsxCellHandler; +import com.alibaba.excel.constant.ExcelXmlConstants; +import com.alibaba.excel.context.xlsx.XlsxReadContext; + +/** + * @author jipengfei + */ +public class XlsxRowHandler extends DefaultHandler { + private XlsxReadContext xlsxReadContext; + private static final Map XLSX_CELL_HANDLER_MAP = new HashMap(16); + + static { + XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.DIMENSION, new CountRowCellHandler()); + XLSX_CELL_HANDLER_MAP.put(ExcelXmlConstants.ROW_TAG, new ProcessResultCellHandler()); + } + + public XlsxRowHandler(XlsxReadContext xlsxReadContext) { + this.xlsxReadContext = xlsxReadContext; + } + + @Override + public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { + XlsxCellHandler handler = XLSX_CELL_HANDLER_MAP.get(name); + if (handler == null) { + return; + } + handler.startHandle(xlsxReadContext, name, attributes); + } + + @Override + public void characters(char[] ch, int start, int length) throws SAXException { + if (rowResultHolder != null) { + rowResultHolder.appendCurrentCellValue(ch, start, length); + } + } + + @Override + public void endElement(String uri, String localName, String name) throws SAXException { + XlsxCellHandler handler = XLSX_CELL_HANDLER_MAP.get(name); + if (handler == null) { + return; + } + handler.endHandle(xlsxReadContext, name); + } + + +} diff --git a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 8172b65..2b5a766 100644 --- a/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -19,7 +19,7 @@ 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.read.processor.DefaultAnalysisEventProcessor; import com.alibaba.excel.support.ExcelTypeEnum; /** @@ -64,7 +64,7 @@ public class AnalysisContextImpl implements AnalysisContext { break; } currentReadHolder = readWorkbookHolder; - analysisEventProcessor = new DefalutAnalysisEventProcessor(); + analysisEventProcessor = new DefaultAnalysisEventProcessor(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Initialization 'AnalysisContextImpl' complete"); } diff --git a/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java b/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java index 924cafe..58d8123 100644 --- a/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java +++ b/src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java @@ -1,8 +1,6 @@ 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; 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 index b7439c3..2e1174d 100644 --- 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 @@ -10,6 +10,9 @@ import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; * @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 index 34ec6ad..9542366 100644 --- 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 @@ -3,6 +3,7 @@ package com.alibaba.excel.read.metadata.holder.xlsx; import javax.xml.parsers.SAXParserFactory; import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.xssf.model.StylesTable; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; @@ -28,6 +29,10 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { * @see SAXParserFactory#newInstance(String, ClassLoader) */ private String saxParserFactoryName; + /** + * Current style information + */ + private StylesTable stylesTable; public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); @@ -49,4 +54,12 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder { public void setSaxParserFactoryName(String saxParserFactoryName) { this.saxParserFactoryName = saxParserFactoryName; } + + public StylesTable getStylesTable() { + return stylesTable; + } + + public void setStylesTable(StylesTable stylesTable) { + this.stylesTable = stylesTable; + } } diff --git a/src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java b/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java similarity index 96% rename from src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java rename to src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java index e673913..6b5b0af 100644 --- a/src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java +++ b/src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java @@ -25,12 +25,11 @@ import com.alibaba.excel.util.StringUtils; * * @author jipengfei */ -public class DefalutAnalysisEventProcessor implements AnalysisEventProcessor { - private static final Logger LOGGER = LoggerFactory.getLogger(DefalutAnalysisEventProcessor.class); +public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor { + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAnalysisEventProcessor.class); @Override public void endRow(AnalysisContext analysisContext) { - switch (analysisContext.readRowHolder().getRowType()) { case EMPTY: if (LOGGER.isDebugEnabled()) { @@ -61,11 +60,14 @@ public class DefalutAnalysisEventProcessor implements AnalysisEventProcessor { } } - private void dealExtra(AnalysisContext analysisContext) {} + private void dealExtra(AnalysisContext analysisContext) { + + + } private void dealData(AnalysisContext analysisContext) { ReadRowHolder readRowHolder = analysisContext.readRowHolder(); - Map cellDataMap = (Map)readRowHolder.getCellMap(); + Map cellDataMap = (Map) readRowHolder.getCellMap(); readRowHolder.setCurrentRowAnalysisResult(cellDataMap); int rowIndex = readRowHolder.getRowIndex(); int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber(); diff --git a/src/main/java/com/alibaba/excel/util/SheetUtils.java b/src/main/java/com/alibaba/excel/util/SheetUtils.java index 492789c..fb2c59e 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.xls.XlsReadContext; +import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.read.metadata.ReadSheet; /** @@ -14,21 +14,21 @@ import com.alibaba.excel.read.metadata.ReadSheet; public class SheetUtils { private static final Logger LOGGER = LoggerFactory.getLogger(SheetUtils.class); - private SheetUtils() {} + private SheetUtils() { + } /** * Match the parameters to the actual sheet * - * @param readSheet - * actual sheet - * @param xlsReadContext + * @param readSheet actual sheet + * @param analysisContext * @return */ - public static ReadSheet match(ReadSheet readSheet, XlsReadContext xlsReadContext) { - if (xlsReadContext.readAll()) { + public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) { + if (analysisContext.readAll()) { return readSheet; } - for (ReadSheet parameterReadSheet : xlsReadContext.readSheetList()) { + for (ReadSheet parameterReadSheet : analysisContext.readSheetList()) { if (parameterReadSheet == null) { continue; } @@ -45,7 +45,7 @@ public class SheetUtils { if (!StringUtils.isEmpty(parameterSheetName)) { boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) || (parameterReadSheet.getAutoTrim() == null - && xlsReadContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); + && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); if (autoTrim) { parameterSheetName = parameterSheetName.trim(); }