forked from fanruan/easyexcel
Jiaju Zhuang
5 years ago
73 changed files with 1147 additions and 515 deletions
@ -0,0 +1,19 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.XlsRecordHandler; |
||||
import com.alibaba.excel.context.xls.XlsReadContext; |
||||
|
||||
/** |
||||
* Abstract xls record handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public abstract class AbstractXlsRecordHandler implements XlsRecordHandler { |
||||
|
||||
@Override |
||||
public boolean support(XlsReadContext xlsReadContext, Record record) { |
||||
return true; |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.HyperlinkRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.xls.XlsReadContext; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class HyperlinkRecordHandler extends AbstractXlsRecordHandler implements IgnorableXlsRecordHandler { |
||||
@Override |
||||
public boolean support(XlsReadContext xlsReadContext, Record record) { |
||||
return xlsReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.HYPERLINK); |
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
HyperlinkRecord hr = (HyperlinkRecord)record; |
||||
CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.HYPERLINK, hr.getAddress(), hr.getFirstRow(), |
||||
hr.getFirstColumn(), hr.getLastRow(), hr.getLastColumn()); |
||||
xlsReadContext.xlsReadSheetHolder().setCellExtra(cellExtra); |
||||
xlsReadContext.analysisEventProcessor().extra(xlsReadContext); |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.excel.analysis.v03.handlers; |
||||
|
||||
import org.apache.poi.hssf.record.MergeCellsRecord; |
||||
import org.apache.poi.hssf.record.Record; |
||||
import org.apache.poi.ss.util.CellRangeAddress; |
||||
|
||||
import com.alibaba.excel.analysis.v03.IgnorableXlsRecordHandler; |
||||
import com.alibaba.excel.context.xls.XlsReadContext; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
|
||||
/** |
||||
* Record handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class MergeCellsRecordHandler extends AbstractXlsRecordHandler implements IgnorableXlsRecordHandler { |
||||
|
||||
@Override |
||||
public boolean support(XlsReadContext xlsReadContext, Record record) { |
||||
return xlsReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.MERGE); |
||||
} |
||||
|
||||
@Override |
||||
public void processRecord(XlsReadContext xlsReadContext, Record record) { |
||||
MergeCellsRecord mcr = (MergeCellsRecord)record; |
||||
for (int i = 0; i < mcr.getNumAreas(); i++) { |
||||
CellRangeAddress cellRangeAddress = mcr.getAreaAt(i); |
||||
CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.MERGE, null, cellRangeAddress.getFirstRow(), |
||||
cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastRow(), cellRangeAddress.getLastColumn()); |
||||
xlsReadContext.xlsReadSheetHolder().setCellExtra(cellExtra); |
||||
xlsReadContext.analysisEventProcessor().extra(xlsReadContext); |
||||
} |
||||
} |
||||
} |
@ -1,28 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v07; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* Build handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public class XlsxHandlerFactory { |
||||
public static List<XlsxCellHandler> buildCellHandlers(AnalysisContext analysisContext, StylesTable stylesTable) { |
||||
List<XlsxCellHandler> result = new ArrayList<XlsxCellHandler>(); |
||||
result.add(new CountRowCellHandler(analysisContext)); |
||||
DefaultCellHandler defaultCellHandler = new DefaultCellHandler(analysisContext, stylesTable); |
||||
result.add(defaultCellHandler); |
||||
result.add(new ProcessResultCellHandler(analysisContext, defaultCellHandler)); |
||||
return result; |
||||
} |
||||
} |
@ -1,33 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v07; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Result holder |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public interface XlsxRowResultHolder { |
||||
/** |
||||
* Clear Result |
||||
*/ |
||||
void clearResult(); |
||||
|
||||
/** |
||||
* Append current 'cellValue' |
||||
* |
||||
* @param ch |
||||
* @param start |
||||
* @param length |
||||
*/ |
||||
void appendCurrentCellValue(char[] ch, int start, int length); |
||||
|
||||
/** |
||||
* Get row content |
||||
* |
||||
* @return |
||||
*/ |
||||
Map<Integer, CellData> getCurRowContent(); |
||||
} |
@ -0,0 +1,64 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import java.math.BigDecimal; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; |
||||
import com.alibaba.excel.util.BooleanUtils; |
||||
|
||||
/** |
||||
* Cell Value Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public abstract class AbstractCellValueTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) { |
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
||||
CellData tempCellData = xlsxReadSheetHolder.getTempCellData(); |
||||
StringBuilder tempData = xlsxReadSheetHolder.getTempData(); |
||||
CellDataTypeEnum oldType = tempCellData.getType(); |
||||
switch (oldType) { |
||||
case DIRECT_STRING: |
||||
case STRING: |
||||
case ERROR: |
||||
tempCellData.setStringValue(tempData.toString()); |
||||
break; |
||||
case BOOLEAN: |
||||
tempCellData.setBooleanValue(BooleanUtils.valueOf(tempData.toString())); |
||||
break; |
||||
case NUMBER: |
||||
case EMPTY: |
||||
tempCellData.setType(CellDataTypeEnum.NUMBER); |
||||
tempCellData.setNumberValue(new BigDecimal(tempData.toString())); |
||||
break; |
||||
default: |
||||
throw new IllegalStateException("Cannot set values now"); |
||||
} |
||||
|
||||
// set string value
|
||||
setStringValue(xlsxReadContext); |
||||
|
||||
if (tempCellData.getStringValue() != null |
||||
&& xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) { |
||||
tempCellData.setStringValue(tempCellData.getStringValue()); |
||||
} |
||||
|
||||
tempCellData.checkEmpty(); |
||||
xlsxReadSheetHolder.getCellMap().put(xlsxReadSheetHolder.getColumnIndex(), tempCellData); |
||||
} |
||||
|
||||
@Override |
||||
public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) { |
||||
xlsxReadContext.xlsxReadSheetHolder().getTempData().append(ch, start, length); |
||||
} |
||||
|
||||
/** |
||||
* Set string value. |
||||
*/ |
||||
protected abstract void setStringValue(XlsxReadContext xlsxReadContext); |
||||
|
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
|
||||
/** |
||||
* Abstract tag handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public abstract class AbstractXlsxTagHandler implements XlsxTagHandler { |
||||
@Override |
||||
public boolean support(XlsxReadContext xlsxReadContext) { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) { |
||||
|
||||
} |
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class CellFormulaTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
||||
xlsxReadSheetHolder.getTempCellData().setFormula(Boolean.TRUE); |
||||
xlsxReadSheetHolder.setTempFormula(new StringBuilder()); |
||||
} |
||||
|
||||
@Override |
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) { |
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
||||
xlsxReadSheetHolder.getTempCellData().setFormulaValue(xlsxReadSheetHolder.getTempFormula().toString()); |
||||
} |
||||
|
||||
@Override |
||||
public void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length) { |
||||
xlsxReadContext.xlsxReadSheetHolder().getTempFormula().append(ch, start, length); |
||||
} |
||||
} |
@ -0,0 +1,23 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Cell inline string value handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class CellInlineStringValueTagHandler extends AbstractCellValueTagHandler { |
||||
|
||||
@Override |
||||
protected void setStringValue(XlsxReadContext xlsxReadContext) { |
||||
// This is a special form of string
|
||||
CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData(); |
||||
XSSFRichTextString richTextString = new XSSFRichTextString(tempCellData.getStringValue()); |
||||
tempCellData.setStringValue(richTextString.toString()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,53 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_DATA_FORMAT_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; |
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle; |
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.constant.BuiltinFormats; |
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadSheetHolder; |
||||
import com.alibaba.excel.util.PositionUtils; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class CellTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
XlsxReadSheetHolder xlsxReadSheetHolder = xlsxReadContext.xlsxReadSheetHolder(); |
||||
xlsxReadSheetHolder.setColumnIndex(PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION))); |
||||
|
||||
// t="s" ,it's means String
|
||||
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
|
||||
// t="inlineStr" ,it's means String
|
||||
// t="b" ,it's means Boolean
|
||||
// t="e" ,it's means Error
|
||||
// t="n" ,it's means Number
|
||||
// t is null ,it's means Empty or Number
|
||||
CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(CELL_VALUE_TYPE_TAG)); |
||||
xlsxReadSheetHolder.setTempCellData(new CellData(type)); |
||||
xlsxReadSheetHolder.setTempData(new StringBuilder()); |
||||
|
||||
// Put in data transformation information
|
||||
String dateFormatIndex = attributes.getValue(CELL_DATA_FORMAT_TAG); |
||||
if (dateFormatIndex != null) { |
||||
int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); |
||||
XSSFCellStyle xssfCellStyle = |
||||
xlsxReadContext.xlsxReadWorkbookHolder().getStylesTable().getStyleAt(dateFormatIndexInteger); |
||||
int dataFormat = xssfCellStyle.getDataFormat(); |
||||
xlsxReadSheetHolder.getTempCellData().setDataFormat(dataFormat); |
||||
xlsxReadSheetHolder.getTempCellData().setDataFormatString(BuiltinFormats.getBuiltinFormat(dataFormat, |
||||
xssfCellStyle.getDataFormatString(), xlsxReadSheetHolder.getGlobalConfiguration().getLocale())); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
|
||||
/** |
||||
* Cell Value Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class CellValueTagHandler extends AbstractCellValueTagHandler { |
||||
|
||||
@Override |
||||
protected void setStringValue(XlsxReadContext xlsxReadContext) { |
||||
// Have to go "sharedStrings.xml" and get it
|
||||
CellData tempCellData = xlsxReadContext.xlsxReadSheetHolder().getTempCellData(); |
||||
switch (tempCellData.getType()) { |
||||
case STRING: |
||||
String stringValue = xlsxReadContext.readWorkbookHolder().getReadCache() |
||||
.get(Integer.valueOf(tempCellData.getStringValue())); |
||||
if (stringValue != null && xlsxReadContext.currentReadHolder().globalConfiguration().getAutoTrim()) { |
||||
stringValue = stringValue.trim(); |
||||
} |
||||
tempCellData.setStringValue(stringValue); |
||||
break; |
||||
case DIRECT_STRING: |
||||
tempCellData.setType(CellDataTypeEnum.STRING); |
||||
break; |
||||
default: |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,169 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_DATA_FORMAT_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_FORMULA_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_INLINE_STRING_VALUE_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TAG; |
||||
import static com.alibaba.excel.constant.ExcelXmlConstants.CELL_VALUE_TYPE_TAG; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.Deque; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.Map; |
||||
|
||||
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.XlsxRowResultHolder; |
||||
import com.alibaba.excel.constant.BuiltinFormats; |
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.util.BooleanUtils; |
||||
import com.alibaba.excel.util.PositionUtils; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder { |
||||
private final AnalysisContext analysisContext; |
||||
private Deque<String> currentTagDeque = new LinkedList<String>(); |
||||
private int curCol; |
||||
private Map<Integer, CellData> curRowContent = new LinkedHashMap<Integer, CellData>(); |
||||
private CellData currentCellData; |
||||
private StringBuilder dataStringBuilder; |
||||
private StringBuilder formulaStringBuilder; |
||||
|
||||
@Override |
||||
public void clearResult() { |
||||
curRowContent = new LinkedHashMap<Integer, CellData>(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean support(String name) { |
||||
return CELL_VALUE_TAG.equals(name) || CELL_FORMULA_TAG.equals(name) || CELL_INLINE_STRING_VALUE_TAG.equals(name) |
||||
|| CELL_TAG.equals(name); |
||||
} |
||||
|
||||
@Override |
||||
public void startHandle(String name, Attributes attributes) { |
||||
currentTagDeque.push(name); |
||||
// start a cell
|
||||
if (CELL_TAG.equals(name)) { |
||||
curCol = PositionUtils.getCol(attributes.getValue(ExcelXmlConstants.POSITION)); |
||||
|
||||
// t="s" ,it's means String
|
||||
// t="str" ,it's means String,but does not need to be read in the 'sharedStrings.xml'
|
||||
// t="inlineStr" ,it's means String
|
||||
// t="b" ,it's means Boolean
|
||||
// t="e" ,it's means Error
|
||||
// t="n" ,it's means Number
|
||||
// t is null ,it's means Empty or Number
|
||||
CellDataTypeEnum type = CellDataTypeEnum.buildFromCellType(attributes.getValue(CELL_VALUE_TYPE_TAG)); |
||||
currentCellData = new CellData(type); |
||||
dataStringBuilder = new StringBuilder(); |
||||
|
||||
// Put in data transformation information
|
||||
String dateFormatIndex = attributes.getValue(CELL_DATA_FORMAT_TAG); |
||||
if (dateFormatIndex != null) { |
||||
int dateFormatIndexInteger = Integer.parseInt(dateFormatIndex); |
||||
XSSFCellStyle xssfCellStyle = stylesTable.getStyleAt(dateFormatIndexInteger); |
||||
int dataFormat = xssfCellStyle.getDataFormat(); |
||||
currentCellData.setDataFormat(dataFormat); |
||||
currentCellData.setDataFormatString( |
||||
BuiltinFormats.getBuiltinFormat(dataFormat, xssfCellStyle.getDataFormatString(), |
||||
analysisContext.readSheetHolder().getGlobalConfiguration().getLocale())); |
||||
} |
||||
} |
||||
// cell is formula
|
||||
if (CELL_FORMULA_TAG.equals(name)) { |
||||
currentCellData.setFormula(Boolean.TRUE); |
||||
formulaStringBuilder = new StringBuilder(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void endHandle(String name) { |
||||
currentTagDeque.pop(); |
||||
// cell is formula
|
||||
if (CELL_FORMULA_TAG.equals(name)) { |
||||
currentCellData.setFormulaValue(formulaStringBuilder.toString()); |
||||
return; |
||||
} |
||||
if (CELL_VALUE_TAG.equals(name) || CELL_INLINE_STRING_VALUE_TAG.equals(name)) { |
||||
CellDataTypeEnum oldType = currentCellData.getType(); |
||||
switch (oldType) { |
||||
case DIRECT_STRING: |
||||
case STRING: |
||||
case ERROR: |
||||
currentCellData.setStringValue(dataStringBuilder.toString()); |
||||
break; |
||||
case BOOLEAN: |
||||
currentCellData.setBooleanValue(BooleanUtils.valueOf(dataStringBuilder.toString())); |
||||
break; |
||||
case NUMBER: |
||||
case EMPTY: |
||||
currentCellData.setType(CellDataTypeEnum.NUMBER); |
||||
currentCellData.setNumberValue(new BigDecimal(dataStringBuilder.toString())); |
||||
break; |
||||
default: |
||||
throw new IllegalStateException("Cannot set values now"); |
||||
} |
||||
|
||||
if (CELL_VALUE_TAG.equals(name)) { |
||||
// Have to go "sharedStrings.xml" and get it
|
||||
if (currentCellData.getType() == CellDataTypeEnum.STRING) { |
||||
String stringValue = analysisContext.readWorkbookHolder().getReadCache() |
||||
.get(Integer.valueOf(currentCellData.getStringValue())); |
||||
if (stringValue != null |
||||
&& analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { |
||||
stringValue = stringValue.trim(); |
||||
} |
||||
currentCellData.setStringValue(stringValue); |
||||
} else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) { |
||||
currentCellData.setType(CellDataTypeEnum.STRING); |
||||
} |
||||
} |
||||
// This is a special form of string
|
||||
if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { |
||||
XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue()); |
||||
String stringValue = richTextString.toString(); |
||||
if (stringValue != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) { |
||||
stringValue = stringValue.trim(); |
||||
} |
||||
currentCellData.setStringValue(stringValue); |
||||
} |
||||
|
||||
currentCellData.checkEmpty(); |
||||
curRowContent.put(curCol, currentCellData); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void appendCurrentCellValue(char[] ch, int start, int length) { |
||||
String currentTag = currentTagDeque.peek(); |
||||
if (currentTag == null) { |
||||
return; |
||||
} |
||||
if (CELL_FORMULA_TAG.equals(currentTag)) { |
||||
formulaStringBuilder.append(ch, start, length); |
||||
return; |
||||
} |
||||
if (!CELL_VALUE_TAG.equals(currentTag) && !CELL_INLINE_STRING_VALUE_TAG.equals(currentTag)) { |
||||
return; |
||||
} |
||||
dataStringBuilder.append(ch, start, length); |
||||
} |
||||
|
||||
@Override |
||||
public Map<Integer, CellData> getCurRowContent() { |
||||
return curRowContent; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class HyperlinkTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public boolean support(XlsxReadContext xlsxReadContext) { |
||||
return xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.HYPERLINK); |
||||
} |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
String ref = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_REF); |
||||
String location = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_LOCATION); |
||||
if (StringUtils.isEmpty(ref)) { |
||||
return; |
||||
} |
||||
CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.HYPERLINK, location, ref); |
||||
xlsxReadContext.readSheetHolder().setCellExtra(cellExtra); |
||||
xlsxReadContext.analysisEventProcessor().extra(xlsxReadContext); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
import com.alibaba.excel.util.StringUtils; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class MergeCellTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public boolean support(XlsxReadContext xlsxReadContext) { |
||||
return xlsxReadContext.readWorkbookHolder().getExtraReadSet().contains(CellExtraTypeEnum.MERGE); |
||||
} |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
String ref = attributes.getValue(ExcelXmlConstants.ATTRIBUTE_REF); |
||||
if (StringUtils.isEmpty(ref)) { |
||||
return; |
||||
} |
||||
CellExtra cellExtra = new CellExtra(CellExtraTypeEnum.MERGE, null, ref); |
||||
xlsxReadContext.readSheetHolder().setCellExtra(cellExtra); |
||||
xlsxReadContext.analysisEventProcessor().extra(xlsxReadContext); |
||||
} |
||||
|
||||
} |
@ -1,30 +0,0 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
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; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class ProcessResultCellHandler implements XlsxCellHandler { |
||||
|
||||
@Override |
||||
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(XlsxReadContext xlsxReadContext, String name) { |
||||
xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,35 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import java.util.LinkedHashMap; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
import com.alibaba.excel.enums.RowTypeEnum; |
||||
import com.alibaba.excel.metadata.Cell; |
||||
import com.alibaba.excel.read.metadata.holder.ReadRowHolder; |
||||
import com.alibaba.excel.util.PositionUtils; |
||||
|
||||
/** |
||||
* Cell Handler |
||||
* |
||||
* @author jipengfei |
||||
*/ |
||||
public class RowTagHandler extends AbstractXlsxTagHandler { |
||||
|
||||
@Override |
||||
public void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes) { |
||||
xlsxReadContext.readRowHolder( |
||||
new ReadRowHolder(PositionUtils.getRowByRowTagt(attributes.getValue(ExcelXmlConstants.POSITION)), |
||||
RowTypeEnum.DATA, xlsxReadContext.readSheetHolder().getGlobalConfiguration(), null)); |
||||
} |
||||
|
||||
@Override |
||||
public void endElement(XlsxReadContext xlsxReadContext, String name) { |
||||
xlsxReadContext.readRowHolder().setCellMap(xlsxReadContext.xlsxReadSheetHolder().getCellMap()); |
||||
xlsxReadContext.analysisEventProcessor().endRow(xlsxReadContext); |
||||
xlsxReadContext.xlsxReadSheetHolder().setCellMap(new LinkedHashMap<Integer, Cell>()); |
||||
} |
||||
|
||||
} |
@ -1,29 +0,0 @@
|
||||
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); |
||||
} |
@ -0,0 +1,53 @@
|
||||
package com.alibaba.excel.analysis.v07.handlers; |
||||
|
||||
import org.xml.sax.Attributes; |
||||
|
||||
import com.alibaba.excel.context.xlsx.XlsxReadContext; |
||||
|
||||
/** |
||||
* Tag handler |
||||
* |
||||
* @author Dan Zheng |
||||
*/ |
||||
public interface XlsxTagHandler { |
||||
|
||||
/** |
||||
* Whether to support |
||||
* |
||||
* @param xlsxReadContext |
||||
*/ |
||||
boolean support(XlsxReadContext xlsxReadContext); |
||||
|
||||
/** |
||||
* Start handle |
||||
* |
||||
* @param xlsxReadContext |
||||
* xlsxReadContext |
||||
* @param name |
||||
* Tag name |
||||
* @param attributes |
||||
* Tag attributes |
||||
*/ |
||||
void startElement(XlsxReadContext xlsxReadContext, String name, Attributes attributes); |
||||
|
||||
/** |
||||
* End handle |
||||
* |
||||
* @param xlsxReadContext |
||||
* xlsxReadContext |
||||
* @param name |
||||
* Tag name |
||||
*/ |
||||
void endElement(XlsxReadContext xlsxReadContext, String name); |
||||
|
||||
/** |
||||
* Read data |
||||
* |
||||
* @param xlsxReadContext |
||||
* @param ch |
||||
* @param start |
||||
* @param length |
||||
*/ |
||||
void characters(XlsxReadContext xlsxReadContext, char[] ch, int start, int length); |
||||
|
||||
} |
@ -0,0 +1,21 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
/** |
||||
* Extra data type |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public enum CellExtraTypeEnum { |
||||
/** |
||||
* Comment |
||||
*/ |
||||
COMMENT, |
||||
/** |
||||
* Hyperlink |
||||
*/ |
||||
HYPERLINK, |
||||
/** |
||||
* Merge |
||||
*/ |
||||
MERGE,; |
||||
} |
@ -1,13 +0,0 @@
|
||||
package com.alibaba.excel.enums; |
||||
|
||||
/** |
||||
* Read some extra data |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
public enum ExtraReadEnum { |
||||
/** |
||||
* Read the comment |
||||
*/ |
||||
COMMENT,; |
||||
} |
@ -1,18 +1,121 @@
|
||||
package com.alibaba.excel.metadata; |
||||
|
||||
import org.apache.poi.ss.util.CellReference; |
||||
|
||||
import com.alibaba.excel.constant.ExcelXmlConstants; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
|
||||
/** |
||||
* Cell extra information. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class CellExtra extends AbstractCell { |
||||
private String note; |
||||
/** |
||||
* Cell extra type |
||||
*/ |
||||
private CellExtraTypeEnum type; |
||||
/** |
||||
* Cell extra data |
||||
*/ |
||||
private String text; |
||||
/** |
||||
* First row index,if this object is an interval |
||||
*/ |
||||
private Integer firstRowIndex; |
||||
/** |
||||
* First column index,if this object is an interval |
||||
*/ |
||||
private Integer firstColumnIndex; |
||||
/** |
||||
* Last row index,if this object is an interval |
||||
*/ |
||||
private Integer lastRowIndex; |
||||
/** |
||||
* Last column index,if this object is an interval |
||||
*/ |
||||
private Integer lastColumnIndex; |
||||
|
||||
public CellExtra(CellExtraTypeEnum type, String text, String range) { |
||||
super(); |
||||
this.type = type; |
||||
this.text = text; |
||||
String[] ranges = range.split(ExcelXmlConstants.CELL_RANGE_SPLIT); |
||||
CellReference first = new CellReference(ranges[0]); |
||||
CellReference last = first; |
||||
this.firstRowIndex = first.getRow(); |
||||
this.firstColumnIndex = (int)first.getCol(); |
||||
setRowIndex(this.firstRowIndex); |
||||
setColumnIndex(this.firstColumnIndex); |
||||
if (ranges.length > 1) { |
||||
last = new CellReference(ranges[1]); |
||||
} |
||||
this.lastRowIndex = last.getRow(); |
||||
this.lastColumnIndex = (int)last.getCol(); |
||||
} |
||||
|
||||
public CellExtra(CellExtraTypeEnum type, String text, Integer rowIndex, Integer columnIndex) { |
||||
this(type, text, rowIndex, columnIndex, rowIndex, columnIndex); |
||||
} |
||||
|
||||
public CellExtra(CellExtraTypeEnum type, String text, Integer firstRowIndex, Integer firstColumnIndex, |
||||
Integer lastRowIndex, Integer lastColumnIndex) { |
||||
super(); |
||||
setRowIndex(firstRowIndex); |
||||
setColumnIndex(firstColumnIndex); |
||||
this.type = type; |
||||
this.text = text; |
||||
this.firstRowIndex = firstRowIndex; |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
this.lastRowIndex = lastRowIndex; |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
|
||||
public CellExtraTypeEnum getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(CellExtraTypeEnum type) { |
||||
this.type = type; |
||||
} |
||||
|
||||
public String getText() { |
||||
return text; |
||||
} |
||||
|
||||
public void setText(String text) { |
||||
this.text = text; |
||||
} |
||||
|
||||
public Integer getFirstRowIndex() { |
||||
return firstRowIndex; |
||||
} |
||||
|
||||
public void setFirstRowIndex(Integer firstRowIndex) { |
||||
this.firstRowIndex = firstRowIndex; |
||||
} |
||||
|
||||
public Integer getFirstColumnIndex() { |
||||
return firstColumnIndex; |
||||
} |
||||
|
||||
public void setFirstColumnIndex(Integer firstColumnIndex) { |
||||
this.firstColumnIndex = firstColumnIndex; |
||||
} |
||||
|
||||
public Integer getLastRowIndex() { |
||||
return lastRowIndex; |
||||
} |
||||
|
||||
public void setLastRowIndex(Integer lastRowIndex) { |
||||
this.lastRowIndex = lastRowIndex; |
||||
} |
||||
|
||||
public String getNote() { |
||||
return note; |
||||
public Integer getLastColumnIndex() { |
||||
return lastColumnIndex; |
||||
} |
||||
|
||||
public void setNote(String note) { |
||||
this.note = note; |
||||
public void setLastColumnIndex(Integer lastColumnIndex) { |
||||
this.lastColumnIndex = lastColumnIndex; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,14 @@
|
||||
package com.alibaba.easyexcel.test.core.extra; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
public class ExtraData { |
||||
|
||||
private String row1; |
||||
|
||||
private String row2; |
||||
} |
@ -0,0 +1,55 @@
|
||||
package com.alibaba.easyexcel.test.core.extra; |
||||
|
||||
import org.junit.Assert; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.excel.context.AnalysisContext; |
||||
import com.alibaba.excel.event.AnalysisEventListener; |
||||
import com.alibaba.excel.metadata.CellExtra; |
||||
import com.alibaba.fastjson.JSON; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ExtraDataListener extends AnalysisEventListener<ExtraData> { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExtraData.class); |
||||
|
||||
@Override |
||||
public void invoke(ExtraData data, AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public void doAfterAllAnalysed(AnalysisContext context) {} |
||||
|
||||
@Override |
||||
public void extra(CellExtra extra, AnalysisContext context) { |
||||
LOGGER.info("extra data:{}", JSON.toJSONString(extra)); |
||||
switch (extra.getType()) { |
||||
case COMMENT: |
||||
Assert.assertEquals("批注的内容", extra.getText()); |
||||
Assert.assertEquals(4, (int)extra.getRowIndex()); |
||||
Assert.assertEquals(0, (int)extra.getColumnIndex()); |
||||
break; |
||||
case HYPERLINK: |
||||
if ("Sheet1!A1".equals(extra.getText())) { |
||||
Assert.assertEquals(1, (int)extra.getRowIndex()); |
||||
Assert.assertEquals(0, (int)extra.getColumnIndex()); |
||||
} else if ("Sheet2!A1".equals(extra.getText())) { |
||||
Assert.assertEquals(2, (int)extra.getFirstRowIndex()); |
||||
Assert.assertEquals(0, (int)extra.getFirstColumnIndex()); |
||||
Assert.assertEquals(3, (int)extra.getLastRowIndex()); |
||||
Assert.assertEquals(1, (int)extra.getLastColumnIndex()); |
||||
} else { |
||||
Assert.fail("Unknown hyperlink!"); |
||||
} |
||||
break; |
||||
case MERGE: |
||||
Assert.assertEquals(5, (int)extra.getFirstRowIndex()); |
||||
Assert.assertEquals(0, (int)extra.getFirstColumnIndex()); |
||||
Assert.assertEquals(6, (int)extra.getLastRowIndex()); |
||||
Assert.assertEquals(1, (int)extra.getLastColumnIndex()); |
||||
break; |
||||
default: |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,44 @@
|
||||
package com.alibaba.easyexcel.test.core.extra; |
||||
|
||||
import java.io.File; |
||||
|
||||
import org.junit.BeforeClass; |
||||
import org.junit.Test; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil; |
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.alibaba.excel.enums.CellExtraTypeEnum; |
||||
|
||||
/** |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ExtraDataTest { |
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExtraDataTest.class); |
||||
private static File file03; |
||||
private static File file07; |
||||
|
||||
@BeforeClass |
||||
public static void init() { |
||||
file03 = TestFileUtil.readFile("extra" + File.separator + "extra.xls"); |
||||
file07 = TestFileUtil.readFile("extra" + File.separator + "extra.xlsx"); |
||||
} |
||||
|
||||
@Test |
||||
public void t01Read07() { |
||||
read(file07); |
||||
} |
||||
|
||||
@Test |
||||
public void t02Read03() { |
||||
read(file03); |
||||
} |
||||
|
||||
private void read(File file) { |
||||
EasyExcel.read(file, ExtraData.class, new ExtraDataListener()).extraRead(CellExtraTypeEnum.COMMENT) |
||||
.extraRead(CellExtraTypeEnum.HYPERLINK).extraRead(CellExtraTypeEnum.MERGE).sheet().doRead(); |
||||
} |
||||
|
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue