forked from fanruan/easyexcel
Jiaju Zhuang
5 years ago
17 changed files with 186 additions and 283 deletions
@ -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,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); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -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); |
||||||
|
} |
@ -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; |
@ -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); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue