Browse Source

添加支持批注

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
13ca08e1d8
  1. 13
      src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  2. 45
      src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java
  3. 1
      src/main/java/com/alibaba/excel/analysis/v07/XlsxHandlerFactory.java
  4. 81
      src/main/java/com/alibaba/excel/analysis/v07/XlsxRowHandler.java
  5. 96
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  6. 26
      src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java
  7. 17
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  8. 39
      src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java
  9. 29
      src/main/java/com/alibaba/excel/analysis/v07/handlers/XlsxCellHandler.java
  10. 2
      src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java
  11. 58
      src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java
  12. 4
      src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  13. 2
      src/main/java/com/alibaba/excel/context/xlsx/XlsxReadContext.java
  14. 3
      src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadSheetHolder.java
  15. 13
      src/main/java/com/alibaba/excel/read/metadata/holder/xlsx/XlsxReadWorkbookHolder.java
  16. 10
      src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java
  17. 18
      src/main/java/com/alibaba/excel/util/SheetUtils.java

13
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.v03.XlsSaxAnalyser;
import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser; import com.alibaba.excel.analysis.v07.XlsxSaxAnalyser;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.context.AnalysisContextImpl;
import com.alibaba.excel.context.xls.DefaultXlsReadContext; import com.alibaba.excel.context.xls.DefaultXlsReadContext;
import com.alibaba.excel.context.xls.XlsReadContext; 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.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
@ -74,8 +75,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
try { try {
decryptedStream = DocumentFactoryHelper decryptedStream = DocumentFactoryHelper
.getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword()); .getDecryptedStream(poifsFileSystem.getRoot().getFileSystem(), readWorkbook.getPassword());
analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, decryptedStream); analysisContext = xlsxReadContext;
excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, decryptedStream);
return; return;
} finally { } finally {
IOUtils.closeQuietly(decryptedStream); IOUtils.closeQuietly(decryptedStream);
@ -93,8 +95,9 @@ public class ExcelAnalyserImpl implements ExcelAnalyser {
excelReadExecutor = new XlsSaxAnalyser(xlsReadContext); excelReadExecutor = new XlsSaxAnalyser(xlsReadContext);
break; break;
case XLSX: case XLSX:
analysisContext = new AnalysisContextImpl(readWorkbook, ExcelTypeEnum.XLSX); XlsxReadContext xlsxReadContext = new DefaultXlsxReadContext(readWorkbook, ExcelTypeEnum.XLSX);
excelReadExecutor = new XlsxSaxAnalyser(analysisContext, null); analysisContext = xlsxReadContext;
excelReadExecutor = new XlsxSaxAnalyser(xlsxReadContext, null);
break; break;
default: default:
break; break;

45
src/main/java/com/alibaba/excel/analysis/v07/XlsxCellHandler.java

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

1
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.CountRowCellHandler;
import com.alibaba.excel.analysis.v07.handlers.DefaultCellHandler; import com.alibaba.excel.analysis.v07.handlers.DefaultCellHandler;
import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler; import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler;
import com.alibaba.excel.analysis.v07.handlers.XlsxCellHandler;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
/** /**

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

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

96
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.PackageAccess;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xssf.eventusermodel.XSSFReader; 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.apache.poi.xssf.usermodel.XSSFRelation;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr;
@ -27,45 +25,34 @@ import org.xml.sax.InputSource;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import com.alibaba.excel.analysis.ExcelReadExecutor; 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.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.exception.ExcelAnalysisException;
import com.alibaba.excel.read.metadata.ReadSheet; 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.CollectionUtils;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.SheetUtils; import com.alibaba.excel.util.SheetUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
/** /**
*
* @author jipengfei * @author jipengfei
*/ */
public class XlsxSaxAnalyser implements ExcelReadExecutor { public class XlsxSaxAnalyser implements ExcelReadExecutor {
private AnalysisContext analysisContext; private XlsxReadContext xlsxReadContext;
private List<ReadSheet> sheetList; private List<ReadSheet> sheetList;
private Map<Integer, InputStream> sheetMap; private Map<Integer, InputStream> sheetMap;
/** public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception {
* excel comments this.xlsxReadContext = xlsxReadContext;
* key: sheetNo
* value: CommentsTanle
*/
private Map<Integer, CommentsTable> commentsTableMap;
/**
* Current style information
*/
private StylesTable stylesTable;
public XlsxSaxAnalyser(AnalysisContext analysisContext, InputStream decryptedStream) throws Exception {
this.analysisContext = analysisContext;
// Initialize cache // Initialize cache
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder(); XlsxReadWorkbookHolder xlsxReadWorkbookHolder = xlsxReadContext.xlsxReadWorkbookHolder();
OPCPackage pkg = readOpcPackage(readWorkbookHolder, decryptedStream); OPCPackage pkg = readOpcPackage(xlsxReadWorkbookHolder, decryptedStream);
readWorkbookHolder.setOpcPackage(pkg); xlsxReadWorkbookHolder.setOpcPackage(pkg);
ArrayList<PackagePart> packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType()); ArrayList<PackagePart> packageParts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
@ -73,19 +60,18 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
PackagePart sharedStringsTablePackagePart = packageParts.get(0); PackagePart sharedStringsTablePackagePart = packageParts.get(0);
// Specify default cache // Specify default cache
defaultReadCache(readWorkbookHolder, sharedStringsTablePackagePart); defaultReadCache(xlsxReadWorkbookHolder, sharedStringsTablePackagePart);
// Analysis sharedStringsTable.xml // Analysis sharedStringsTable.xml
analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), readWorkbookHolder); analysisSharedStringsTable(sharedStringsTablePackagePart.getInputStream(), xlsxReadWorkbookHolder);
} }
XSSFReader xssfReader = new XSSFReader(pkg); XSSFReader xssfReader = new XSSFReader(pkg);
analysisUse1904WindowDate(xssfReader, readWorkbookHolder); analysisUse1904WindowDate(xssfReader, xlsxReadWorkbookHolder);
stylesTable = xssfReader.getStylesTable(); xlsxReadWorkbookHolder.setStylesTable(xssfReader.getStylesTable());
sheetList = new ArrayList<ReadSheet>(); sheetList = new ArrayList<ReadSheet>();
sheetMap = new HashMap<Integer, InputStream>(); sheetMap = new HashMap<Integer, InputStream>();
commentsTableMap = new HashMap<Integer, CommentsTable>();
XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator) xssfReader.getSheetsData(); XSSFReader.SheetIterator ite = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
int index = 0; int index = 0;
if (!ite.hasNext()) { if (!ite.hasNext()) {
@ -95,23 +81,19 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
InputStream inputStream = ite.next(); InputStream inputStream = ite.next();
sheetList.add(new ReadSheet(index, ite.getSheetName())); sheetList.add(new ReadSheet(index, ite.getSheetName()));
sheetMap.put(index, inputStream); sheetMap.put(index, inputStream);
CommentsTable commentsTable = ite.getSheetComments();
if (null != commentsTable) {
commentsTableMap.put(index, commentsTable);
}
index++; index++;
} }
} }
private void defaultReadCache(ReadWorkbookHolder readWorkbookHolder, PackagePart sharedStringsTablePackagePart) { private void defaultReadCache(XlsxReadWorkbookHolder xlsxReadWorkbookHolder, PackagePart sharedStringsTablePackagePart) {
ReadCache readCache = readWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart); ReadCache readCache = xlsxReadWorkbookHolder.getReadCacheSelector().readCache(sharedStringsTablePackagePart);
readWorkbookHolder.setReadCache(readCache); xlsxReadWorkbookHolder.setReadCache(readCache);
readCache.init(analysisContext); readCache.init(xlsxReadContext);
} }
private void analysisUse1904WindowDate(XSSFReader xssfReader, ReadWorkbookHolder readWorkbookHolder) private void analysisUse1904WindowDate(XSSFReader xssfReader, XlsxReadWorkbookHolder xlsxReadWorkbookHolder)
throws Exception { throws Exception {
if (readWorkbookHolder.globalConfiguration().getUse1904windowing() != null) { if (xlsxReadWorkbookHolder.globalConfiguration().getUse1904windowing() != null) {
return; return;
} }
InputStream workbookXml = xssfReader.getWorkbookData(); InputStream workbookXml = xssfReader.getWorkbookData();
@ -119,38 +101,38 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
CTWorkbook wb = ctWorkbook.getWorkbook(); CTWorkbook wb = ctWorkbook.getWorkbook();
CTWorkbookPr prefix = wb.getWorkbookPr(); CTWorkbookPr prefix = wb.getWorkbookPr();
if (prefix != null && prefix.getDate1904()) { if (prefix != null && prefix.getDate1904()) {
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE);
} else { } else {
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE); xlsxReadWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
} }
} }
private void analysisSharedStringsTable(InputStream sharedStringsTableInputStream, private void analysisSharedStringsTable(InputStream sharedStringsTableInputStream,
ReadWorkbookHolder readWorkbookHolder) throws Exception { XlsxReadWorkbookHolder xlsxReadWorkbookHolder) throws Exception {
ContentHandler handler = new SharedStringsTableHandler(readWorkbookHolder.getReadCache()); ContentHandler handler = new SharedStringsTableHandler(xlsxReadWorkbookHolder.getReadCache());
parseXmlSource(sharedStringsTableInputStream, handler); 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 { throws Exception {
if (decryptedStream == null && readWorkbookHolder.getFile() != null) { if (decryptedStream == null && xlsxReadWorkbookHolder.getFile() != null) {
return OPCPackage.open(readWorkbookHolder.getFile()); return OPCPackage.open(xlsxReadWorkbookHolder.getFile());
} }
if (readWorkbookHolder.getMandatoryUseInputStream()) { if (xlsxReadWorkbookHolder.getMandatoryUseInputStream()) {
if (decryptedStream != null) { if (decryptedStream != null) {
return OPCPackage.open(decryptedStream); return OPCPackage.open(decryptedStream);
} else { } else {
return OPCPackage.open(readWorkbookHolder.getInputStream()); return OPCPackage.open(xlsxReadWorkbookHolder.getInputStream());
} }
} }
File readTempFile = FileUtils.createCacheTmpFile(); File readTempFile = FileUtils.createCacheTmpFile();
readWorkbookHolder.setTempFile(readTempFile); xlsxReadWorkbookHolder.setTempFile(readTempFile);
File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx"); File tempFile = new File(readTempFile.getPath(), UUID.randomUUID().toString() + ".xlsx");
if (decryptedStream != null) { if (decryptedStream != null) {
FileUtils.writeToFile(tempFile, decryptedStream); FileUtils.writeToFile(tempFile, decryptedStream);
} else { } else {
FileUtils.writeToFile(tempFile, readWorkbookHolder.getInputStream()); FileUtils.writeToFile(tempFile, xlsxReadWorkbookHolder.getInputStream());
} }
return OPCPackage.open(tempFile, PackageAccess.READ); return OPCPackage.open(tempFile, PackageAccess.READ);
} }
@ -164,7 +146,7 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
InputSource inputSource = new InputSource(inputStream); InputSource inputSource = new InputSource(inputStream);
try { try {
SAXParserFactory saxFactory; SAXParserFactory saxFactory;
String xlsxSAXParserFactoryName = analysisContext.readWorkbookHolder().getXlsxSAXParserFactoryName(); String xlsxSAXParserFactoryName = xlsxReadContext.xlsxReadWorkbookHolder().getSaxParserFactoryName();
if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) { if (StringUtils.isEmpty(xlsxSAXParserFactoryName)) {
saxFactory = SAXParserFactory.newInstance(); saxFactory = SAXParserFactory.newInstance();
} else { } else {
@ -194,16 +176,14 @@ public class XlsxSaxAnalyser implements ExcelReadExecutor {
} }
@Override @Override
public void execute(List<ReadSheet> readSheetList, Boolean readAll) { public void execute() {
for (ReadSheet readSheet : sheetList) { for (ReadSheet readSheet : sheetList) {
readSheet = SheetUtils.match(readSheet, readSheetList, readAll, readSheet = SheetUtils.match(readSheet, xlsxReadContext);
analysisContext.readWorkbookHolder().getGlobalConfiguration());
if (readSheet != null) { if (readSheet != null) {
analysisContext.currentSheet(readSheet); xlsxReadContext.currentSheet(readSheet);
Integer sheetNo = readSheet.getSheetNo(); parseXmlSource(sheetMap.get(readSheet.getSheetNo()), new XlsxRowHandler(xlsxReadContext));
parseXmlSource(sheetMap.get(sheetNo), new XlsxRowHandler(analysisContext, stylesTable, commentsTableMap.get(sheetNo)));
// The last sheet is read // The last sheet is read
analysisContext.readSheetHolder().notifyAfterAllAnalysed(analysisContext); xlsxReadContext.analysisEventProcessor().endSheet(xlsxReadContext);
} }
} }
} }

26
src/main/java/com/alibaba/excel/analysis/v07/handlers/CountRowCellHandler.java

@ -1,12 +1,10 @@
package com.alibaba.excel.analysis.v07.handlers; 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 static com.alibaba.excel.constant.ExcelXmlConstants.DIMENSION_REF;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import com.alibaba.excel.analysis.v07.XlsxCellHandler; import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.context.AnalysisContext;
/** /**
* Cell Handler * Cell Handler
@ -15,33 +13,17 @@ import com.alibaba.excel.context.AnalysisContext;
*/ */
public class CountRowCellHandler implements XlsxCellHandler { 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 @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
String d = attributes.getValue(DIMENSION_REF); String d = attributes.getValue(DIMENSION_REF);
String totalStr = d.substring(d.indexOf(":") + 1, d.length()); String totalStr = d.substring(d.indexOf(":") + 1, d.length());
String c = totalStr.toUpperCase().replaceAll("[A-Z]", ""); String c = totalStr.toUpperCase().replaceAll("[A-Z]", "");
analysisContext.readSheetHolder().setApproximateTotalRowNumber(Integer.parseInt(c)); xlsxReadContext.readSheetHolder().setApproximateTotalRowNumber(Integer.parseInt(c));
} }
@Override @Override
public void endHandle(String name) { public void endHandle(XlsxReadContext xlsxReadContext, String name) {
}
@Override
public void handleComments(String comment) {
} }
} }

17
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.LinkedList;
import java.util.Map; import java.util.Map;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import com.alibaba.excel.analysis.v07.XlsxCellHandler;
import com.alibaba.excel.analysis.v07.XlsxRowResultHolder; import com.alibaba.excel.analysis.v07.XlsxRowResultHolder;
import com.alibaba.excel.constant.BuiltinFormats; import com.alibaba.excel.constant.BuiltinFormats;
import com.alibaba.excel.constant.ExcelXmlConstants; import com.alibaba.excel.constant.ExcelXmlConstants;
@ -42,16 +40,6 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
private StringBuilder dataStringBuilder; private StringBuilder dataStringBuilder;
private StringBuilder formulaStringBuilder; private StringBuilder formulaStringBuilder;
/**
* Current style information
*/
private StylesTable stylesTable;
public DefaultCellHandler(AnalysisContext analysisContext, StylesTable stylesTable) {
this.analysisContext = analysisContext;
this.stylesTable = stylesTable;
}
@Override @Override
public void clearResult() { public void clearResult() {
curRowContent = new LinkedHashMap<Integer, CellData>(); curRowContent = new LinkedHashMap<Integer, CellData>();
@ -157,11 +145,6 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
} }
} }
@Override
public void handleComments(String comment) {
// analysisContext.readRowHolder().addComments(curCol, comment);
}
@Override @Override
public void appendCurrentCellValue(char[] ch, int start, int length) { public void appendCurrentCellValue(char[] ch, int start, int length) {
String currentTag = currentTagDeque.peek(); String currentTag = currentTagDeque.peek();

39
src/main/java/com/alibaba/excel/analysis/v07/handlers/ProcessResultCellHandler.java

@ -1,14 +1,10 @@
package com.alibaba.excel.analysis.v07.handlers; package com.alibaba.excel.analysis.v07.handlers;
import static com.alibaba.excel.constant.ExcelXmlConstants.ROW_TAG;
import org.xml.sax.Attributes; 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.constant.ExcelXmlConstants;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.xlsx.XlsxReadContext;
import com.alibaba.excel.read.listener.event.EachRowAnalysisFinishEvent; import com.alibaba.excel.enums.RowTypeEnum;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
@ -18,36 +14,17 @@ import com.alibaba.excel.util.PositionUtils;
* @author jipengfei * @author jipengfei
*/ */
public class ProcessResultCellHandler implements XlsxCellHandler { 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 @Override
public void startHandle(String name, Attributes attributes) { public void startHandle(XlsxReadContext xlsxReadContext, String name, Attributes attributes) {
analysisContext.readRowHolder( xlsxReadContext.readRowHolder(
new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), RowTypeEnum.DATA,
analysisContext.readSheetHolder().getGlobalConfiguration())); xlsxReadContext.readSheetHolder().getGlobalConfiguration()));
} }
@Override @Override
public void endHandle(String name) { public void endHandle(XlsxReadContext xlsxReadContext, String name) {
analysisContext.readSheetHolder() xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext);
.notifyEndOneRow(new EachRowAnalysisFinishEvent(rowResultHandler.getCurRowContent()), analysisContext);
rowResultHandler.clearResult();
}
@Override
public void handleComments(String comment) {
} }
} }

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

2
src/main/java/com/alibaba/excel/analysis/v07/SharedStringsTableHandler.java → 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.Attributes;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;

58
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<String, XlsxCellHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxCellHandler>(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);
}
}

4
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.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;
import com.alibaba.excel.read.processor.AnalysisEventProcessor; import com.alibaba.excel.read.processor.AnalysisEventProcessor;
import com.alibaba.excel.read.processor.DefalutAnalysisEventProcessor; import com.alibaba.excel.read.processor.DefaultAnalysisEventProcessor;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
/** /**
@ -64,7 +64,7 @@ public class AnalysisContextImpl implements AnalysisContext {
break; break;
} }
currentReadHolder = readWorkbookHolder; currentReadHolder = readWorkbookHolder;
analysisEventProcessor = new DefalutAnalysisEventProcessor(); analysisEventProcessor = new DefaultAnalysisEventProcessor();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Initialization 'AnalysisContextImpl' complete"); LOGGER.debug("Initialization 'AnalysisContextImpl' complete");
} }

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

@ -1,8 +1,6 @@
package com.alibaba.excel.context.xlsx; package com.alibaba.excel.context.xlsx;
import com.alibaba.excel.context.AnalysisContext; 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.XlsxReadSheetHolder;
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder;

3
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 * @author Jiaju Zhuang
*/ */
public class XlsxReadSheetHolder extends ReadSheetHolder { public class XlsxReadSheetHolder extends ReadSheetHolder {
public XlsxReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) { public XlsxReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder readWorkbookHolder) {
super(readSheet, readWorkbookHolder); super(readSheet, readWorkbookHolder);
} }

13
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 javax.xml.parsers.SAXParserFactory;
import org.apache.poi.openxml4j.opc.OPCPackage; 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.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
@ -28,6 +29,10 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
* @see SAXParserFactory#newInstance(String, ClassLoader) * @see SAXParserFactory#newInstance(String, ClassLoader)
*/ */
private String saxParserFactoryName; private String saxParserFactoryName;
/**
* Current style information
*/
private StylesTable stylesTable;
public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) { public XlsxReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook); super(readWorkbook);
@ -49,4 +54,12 @@ public class XlsxReadWorkbookHolder extends ReadWorkbookHolder {
public void setSaxParserFactoryName(String saxParserFactoryName) { public void setSaxParserFactoryName(String saxParserFactoryName) {
this.saxParserFactoryName = saxParserFactoryName; this.saxParserFactoryName = saxParserFactoryName;
} }
public StylesTable getStylesTable() {
return stylesTable;
}
public void setStylesTable(StylesTable stylesTable) {
this.stylesTable = stylesTable;
}
} }

10
src/main/java/com/alibaba/excel/read/processor/DefalutAnalysisEventProcessor.java → src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java

@ -25,12 +25,11 @@ import com.alibaba.excel.util.StringUtils;
* *
* @author jipengfei * @author jipengfei
*/ */
public class DefalutAnalysisEventProcessor implements AnalysisEventProcessor { public class DefaultAnalysisEventProcessor implements AnalysisEventProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(DefalutAnalysisEventProcessor.class); private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAnalysisEventProcessor.class);
@Override @Override
public void endRow(AnalysisContext analysisContext) { public void endRow(AnalysisContext analysisContext) {
switch (analysisContext.readRowHolder().getRowType()) { switch (analysisContext.readRowHolder().getRowType()) {
case EMPTY: case EMPTY:
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
@ -61,7 +60,10 @@ public class DefalutAnalysisEventProcessor implements AnalysisEventProcessor {
} }
} }
private void dealExtra(AnalysisContext analysisContext) {} private void dealExtra(AnalysisContext analysisContext) {
}
private void dealData(AnalysisContext analysisContext) { private void dealData(AnalysisContext analysisContext) {
ReadRowHolder readRowHolder = analysisContext.readRowHolder(); ReadRowHolder readRowHolder = analysisContext.readRowHolder();

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

@ -3,7 +3,7 @@ package com.alibaba.excel.util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.xls.XlsReadContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
/** /**
@ -14,21 +14,21 @@ import com.alibaba.excel.read.metadata.ReadSheet;
public class SheetUtils { public class SheetUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(SheetUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(SheetUtils.class);
private SheetUtils() {} private SheetUtils() {
}
/** /**
* Match the parameters to the actual sheet * Match the parameters to the actual sheet
* *
* @param readSheet * @param readSheet actual sheet
* actual sheet * @param analysisContext
* @param xlsReadContext
* @return * @return
*/ */
public static ReadSheet match(ReadSheet readSheet, XlsReadContext xlsReadContext) { public static ReadSheet match(ReadSheet readSheet, AnalysisContext analysisContext) {
if (xlsReadContext.readAll()) { if (analysisContext.readAll()) {
return readSheet; return readSheet;
} }
for (ReadSheet parameterReadSheet : xlsReadContext.readSheetList()) { for (ReadSheet parameterReadSheet : analysisContext.readSheetList()) {
if (parameterReadSheet == null) { if (parameterReadSheet == null) {
continue; continue;
} }
@ -45,7 +45,7 @@ public class SheetUtils {
if (!StringUtils.isEmpty(parameterSheetName)) { if (!StringUtils.isEmpty(parameterSheetName)) {
boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim())
|| (parameterReadSheet.getAutoTrim() == null || (parameterReadSheet.getAutoTrim() == null
&& xlsReadContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
if (autoTrim) { if (autoTrim) {
parameterSheetName = parameterSheetName.trim(); parameterSheetName = parameterSheetName.trim();
} }

Loading…
Cancel
Save