Browse Source

添加支持批注

2.1.x
Jiaju Zhuang 5 years ago
parent
commit
13ca08e1d8
  1. 21
      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. 98
      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. 12
      src/main/java/com/alibaba/excel/read/processor/DefaultAnalysisEventProcessor.java
  17. 18
      src/main/java/com/alibaba/excel/util/SheetUtils.java

21
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;

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.DefaultCellHandler;
import com.alibaba.excel.analysis.v07.handlers.ProcessResultCellHandler;
import com.alibaba.excel.analysis.v07.handlers.XlsxCellHandler;
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);
}
}
}

98
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<ReadSheet> sheetList;
private Map<Integer, InputStream> sheetMap;
/**
* excel comments
* 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;
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<PackagePart> 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<ReadSheet>();
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;
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<ReadSheet> 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);
}
}
}

26
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) {
}
}

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.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<Integer, CellData>();
@ -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();

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

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.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.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");
}

2
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;

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
*/
public class XlsxReadSheetHolder extends ReadSheetHolder {
public XlsxReadSheetHolder(ReadSheet readSheet, ReadWorkbookHolder 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 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;
}
}

12
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
*/
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<Integer, CellData> cellDataMap = (Map)readRowHolder.getCellMap();
Map<Integer, CellData> cellDataMap = (Map) readRowHolder.getCellMap();
readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
int rowIndex = readRowHolder.getRowIndex();
int currentHeadRowNumber = analysisContext.readSheetHolder().getHeadRowNumber();

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.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();
}

Loading…
Cancel
Save