Browse Source

修改转换器的BUG

bugfix
zhuangjiaju 5 years ago
parent
commit
3c76802bd6
  1. 10
      src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  2. 2
      src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java
  3. 11
      src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java
  4. 25
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  5. 4
      src/main/java/com/alibaba/excel/context/WriteContextImpl.java
  6. 18
      src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  7. 2
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java
  8. 2
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java
  9. 7
      src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java
  10. 24
      src/main/java/com/alibaba/excel/metadata/Head.java
  11. 7
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  12. 21
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  13. 4
      src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java
  14. 9
      src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java
  15. 16
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  16. 47
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  17. 3
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java
  18. 1
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  19. 29
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  20. 45
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java
  21. 46
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  22. 53
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadConverterData.java
  23. 2
      src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java
  24. 15
      src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  25. 7
      src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java
  26. BIN
      src/test/resources/converter/converter03.xls
  27. BIN
      src/test/resources/converter/converter07.xlsx

10
src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java

@ -4,6 +4,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
@ -62,7 +64,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
*/
private EventWorkbookBuilder.SheetRecordCollectingListener workbookBuildingListener;
private FormatTrackingHSSFListener formatListener;
private List<CellData> records;
private Map<Integer, CellData> records;
private List<ReadSheet> sheets = new ArrayList<ReadSheet>();
private HSSFWorkbook stubWorkbook;
private List<XlsRecordHandler> recordHandlers = new ArrayList<XlsRecordHandler>();
@ -70,7 +72,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
public XlsSaxAnalyser(AnalysisContext context) throws IOException {
this.analysisContext = context;
this.records = new ArrayList<CellData>();
this.records = new TreeMap<Integer, CellData>();
ReadWorkbookHolder readWorkbookHolder = analysisContext.readWorkbookHolder();
if (readWorkbookHolder.getFile() != null) {
this.fs = new POIFSFileSystem(readWorkbookHolder.getFile());
@ -114,7 +116,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
private void init() {
lastRowNumber = 0;
lastColumnNumber = 0;
records = new ArrayList<CellData>();
records = new TreeMap<Integer, CellData>();
sheets = new ArrayList<ReadSheet>();
buildXlsRecordHandlers();
}
@ -131,7 +133,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelExecutor {
thisColumn = handler.getColumn();
cellData = handler.getCellData();
if (cellData != null) {
records.add(cellData);
records.put(thisColumn, cellData);
}
break;
}

2
src/main/java/com/alibaba/excel/analysis/v03/handlers/BlankOrErrorRecordHandler.java

@ -31,7 +31,7 @@ public class BlankOrErrorRecordHandler extends AbstractXlsRecordHandler {
BoolErrRecord ber = (BoolErrRecord)record;
this.row = ber.getRow();
this.column = ber.getColumn();
this.cellData = new CellData(CellDataTypeEnum.EMPTY);
this.cellData = new CellData(ber.getBooleanValue());
}
}

11
src/main/java/com/alibaba/excel/analysis/v07/XlsxRowResultHolder.java

@ -1,6 +1,6 @@
package com.alibaba.excel.analysis.v07;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.metadata.CellData;
@ -27,12 +27,5 @@ public interface XlsxRowResultHolder {
*
* @return
*/
List<CellData> getCurRowContent();
/**
* get column size
*
* @return
*/
int getColumnSize();
Map<Integer, CellData> getCurRowContent();
}

25
src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java

@ -6,8 +6,8 @@ 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.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
@ -34,7 +34,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
private String currentCellIndex;
private int curRow;
private int curCol;
private List<CellData> curRowContent = new ArrayList<CellData>();
private Map<Integer, CellData> curRowContent = new TreeMap<Integer, CellData>();
private CellData currentCellData;
public DefaultCellHandler(AnalysisContext analysisContext) {
@ -43,7 +43,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
@Override
public void clearResult() {
curRowContent.clear();
curRowContent = new TreeMap<Integer, CellData>();
}
@Override
@ -67,6 +67,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
curCol = PositionUtils.getCol(currentCellIndex);
// 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
@ -92,8 +93,10 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
stringValue = stringValue.trim();
}
currentCellData.setStringValue(stringValue);
} else if (currentCellData.getType() == CellDataTypeEnum.DIRECT_STRING) {
currentCellData.setType(CellDataTypeEnum.STRING);
}
curRowContent.set(curCol, currentCellData);
curRowContent.put(curCol, currentCellData);
}
// This is a special form of string
if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) {
@ -103,7 +106,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
stringValue = stringValue.trim();
}
currentCellData.setStringValue(stringValue);
curRowContent.set(curCol, currentCellData);
curRowContent.put(curCol, currentCellData);
}
}
@ -121,6 +124,7 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
}
CellDataTypeEnum oldType = currentCellData.getType();
switch (oldType) {
case DIRECT_STRING:
case STRING:
case ERROR:
currentCellData.setStringValue(currentCellValue);
@ -139,13 +143,8 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
}
@Override
public List<CellData> getCurRowContent() {
return this.curRowContent;
}
@Override
public int getColumnSize() {
return this.curCol;
public Map<Integer, CellData> getCurRowContent() {
return curRowContent;
}
}

4
src/main/java/com/alibaba/excel/context/WriteContextImpl.java

@ -352,6 +352,10 @@ public class WriteContextImpl implements WriteContext {
if (writeWorkbookHolder.getTemplateInputStream() != null) {
writeWorkbookHolder.getTemplateInputStream().close();
}
} else {
if (writeWorkbookHolder.getFile() != null && writeWorkbookHolder.getOutputStream() != null) {
writeWorkbookHolder.getOutputStream().close();
}
}
} catch (IOException e) {
throw new ExcelGenerateException("Can not close IO", e);

18
src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java

@ -14,16 +14,22 @@ import com.alibaba.excel.converters.byteconverter.ByteNumberConverter;
import com.alibaba.excel.converters.byteconverter.ByteStringConverter;
import com.alibaba.excel.converters.date.DateNumberConverter;
import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.converters.doubleconverter.DoubleBooleanConverter;
import com.alibaba.excel.converters.doubleconverter.DoubleNumberConverter;
import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter;
import com.alibaba.excel.converters.floatconverter.FloatBooleanConverter;
import com.alibaba.excel.converters.floatconverter.FloatNumberConverter;
import com.alibaba.excel.converters.floatconverter.FloatStringConverter;
import com.alibaba.excel.converters.integer.IntegerBooleanConverter;
import com.alibaba.excel.converters.integer.IntegerNumberConverter;
import com.alibaba.excel.converters.integer.IntegerStringConverter;
import com.alibaba.excel.converters.longconverter.LongBooleanConverter;
import com.alibaba.excel.converters.longconverter.LongNumberConverter;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
import com.alibaba.excel.converters.shortconverter.ShortBooleanConverter;
import com.alibaba.excel.converters.shortconverter.ShortNumberConverter;
import com.alibaba.excel.converters.shortconverter.ShortStringConverter;
import com.alibaba.excel.converters.string.StringBooleanConverter;
import com.alibaba.excel.converters.string.StringErrorConverter;
import com.alibaba.excel.converters.string.StringNumberConverter;
import com.alibaba.excel.converters.string.StringStringConverter;
@ -80,27 +86,27 @@ public class DefaultConverterLoader {
putReadConverter(converterMap, new DateNumberConverter());
putReadConverter(converterMap, new DateStringConverter());
putReadConverter(converterMap, new DoubleNumberConverter());
putReadConverter(converterMap, new DoubleBooleanConverter());
putReadConverter(converterMap, new DoubleNumberConverter());
putReadConverter(converterMap, new DoubleStringConverter());
putReadConverter(converterMap, new FloatNumberConverter());
putReadConverter(converterMap, new FloatBooleanConverter());
putReadConverter(converterMap, new FloatNumberConverter());
putReadConverter(converterMap, new FloatStringConverter());
putReadConverter(converterMap, new IntegerNumberConverter());
putReadConverter(converterMap, new IntegerBooleanConverter());
putReadConverter(converterMap, new IntegerNumberConverter());
putReadConverter(converterMap, new IntegerStringConverter());
putReadConverter(converterMap, new LongNumberConverter());
putReadConverter(converterMap, new LongBooleanConverter());
putReadConverter(converterMap, new LongNumberConverter());
putReadConverter(converterMap, new LongStringConverter());
putReadConverter(converterMap, new ShortNumberConverter());
putReadConverter(converterMap, new ShortBooleanConverter());
putReadConverter(converterMap, new ShortNumberConverter());
putReadConverter(converterMap, new ShortStringConverter());
putReadConverter(converterMap, new StringNumberConverter());
putReadConverter(converterMap, new StringBooleanConverter());
putReadConverter(converterMap, new StringNumberConverter());
putReadConverter(converterMap, new StringStringConverter());
putReadConverter(converterMap, new StringErrorConverter());

2
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java

@ -17,7 +17,7 @@ public class BigDecimalBooleanConverter implements Converter<BigDecimal> {
@Override
public Class supportJavaTypeKey() {
return Byte.class;
return BigDecimal.class;
}
@Override

2
src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java

@ -17,7 +17,7 @@ public class ShortBooleanConverter implements Converter<Short> {
@Override
public Class supportJavaTypeKey() {
return Long.class;
return Short.class;
}
@Override

7
src/main/java/com/alibaba/excel/enums/CellDataTypeEnum.java

@ -11,11 +11,15 @@ import com.alibaba.excel.util.StringUtils;
* @author zhuangjiaju
*/
public enum CellDataTypeEnum {
/**
* string
*/
STRING,
/**
* This type of data does not need to be read in the 'sharedStrings.xml', it is only used for overuse, and the data
* will be stored as a {@link #STRING}
*/
DIRECT_STRING,
/**
* number
*/
@ -36,6 +40,7 @@ public enum CellDataTypeEnum {
private static final Map<String, CellDataTypeEnum> TYPE_ROUTING_MAP = new HashMap<String, CellDataTypeEnum>(16);
static {
TYPE_ROUTING_MAP.put("s", STRING);
TYPE_ROUTING_MAP.put("str", DIRECT_STRING);
TYPE_ROUTING_MAP.put("inlineStr", STRING);
TYPE_ROUTING_MAP.put("e", ERROR);
TYPE_ROUTING_MAP.put("b", BOOLEAN);

24
src/main/java/com/alibaba/excel/metadata/Head.java

@ -27,20 +27,17 @@ public class Head {
* Whether index is specified
*/
private Boolean forceIndex;
/**
* Whether to specify a name
*/
private Boolean forceName;
/**
* column with
*/
private ColumnWidthProperty columnWidthProperty;
public Head(Integer columnIndex, String fieldName, String headName) {
this.columnIndex = columnIndex;
this.fieldName = fieldName;
headNameList = new ArrayList<String>();
headNameList.add(headName);
this.forceIndex = Boolean.FALSE;
}
public Head(Integer columnIndex, String fieldName, List<String> headNameList, Boolean forceIndex) {
public Head(Integer columnIndex, String fieldName, List<String> headNameList, Boolean forceIndex,
Boolean forceName) {
this.columnIndex = columnIndex;
this.fieldName = fieldName;
if (headNameList == null) {
@ -48,6 +45,7 @@ public class Head {
}
this.headNameList = headNameList;
this.forceIndex = forceIndex;
this.forceName = forceName;
}
public Integer getColumnIndex() {
@ -89,4 +87,12 @@ public class Head {
public void setForceIndex(Boolean forceIndex) {
this.forceIndex = forceIndex;
}
public Boolean getForceName() {
return forceName;
}
public void setForceName(Boolean forceName) {
this.forceName = forceName;
}
}

7
src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java

@ -60,7 +60,7 @@ public class ExcelHeadProperty {
headRowNumber = 0;
if (head != null && !head.isEmpty()) {
for (int i = 0; i < head.size(); i++) {
headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE));
headMap.put(i, new Head(i, null, head.get(i), Boolean.FALSE, Boolean.TRUE));
contentPropertyMap.put(i, null);
}
headKind = HeadKindEnum.STRING;
@ -155,15 +155,18 @@ public class ExcelHeadProperty {
private void initOneColumnProperty(int index, Field field, Boolean forceIndex) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
List<String> tmpHeadList = new ArrayList<String>();
Boolean forceName = Boolean.TRUE;
if (excelProperty != null) {
tmpHeadList = Arrays.asList(excelProperty.value());
} else {
forceName = Boolean.FALSE;
tmpHeadList.add(field.getName());
}
if (tmpHeadList.isEmpty() || StringUtils.isEmpty(tmpHeadList.get(0))) {
forceName = Boolean.FALSE;
tmpHeadList.add(field.getName());
}
Head head = new Head(index, field.getName(), tmpHeadList, forceIndex);
Head head = new Head(index, field.getName(), tmpHeadList, forceIndex, forceName);
ExcelContentProperty excelContentProperty = new ExcelContentProperty();
if (excelProperty != null && excelProperty.converter() != null) {
Class<? extends Converter> convertClazz = excelProperty.converter();

21
src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java

@ -26,28 +26,28 @@ import net.sf.cglib.beans.BeanMap;
*
* @author jipengfei
*/
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<List<CellData>> {
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<Map<Integer, CellData>> {
@Override
public void invoke(List<CellData> data, AnalysisContext context) {
public void invoke(Map<Integer, CellData> cellDataMap, AnalysisContext context) {
ReadHolder currentReadHolder = context.currentReadHolder();
if (HeadKindEnum.CLASS.equals(currentReadHolder.excelReadHeadProperty().getHeadKind())) {
context.readRowHolder().setCurrentRowAnalysisResult(buildUserModel(data, currentReadHolder));
context.readRowHolder().setCurrentRowAnalysisResult(buildUserModel(cellDataMap, currentReadHolder));
return;
}
context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(data, currentReadHolder));
context.readRowHolder().setCurrentRowAnalysisResult(buildStringList(cellDataMap, currentReadHolder));
}
private Object buildStringList(List<CellData> data, ReadHolder currentReadHolder) {
private Object buildStringList(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder) {
List<String> list = new ArrayList<String>();
for (CellData cellData : data) {
for (CellData cellData : cellDataMap.values()) {
list.add((String)convertValue(cellData, String.class, null, currentReadHolder.converterMap(),
currentReadHolder.globalConfiguration()));
}
return list;
}
private Object buildUserModel(List<CellData> data, ReadHolder currentReadHolder) {
private Object buildUserModel(Map<Integer, CellData> cellDataMap, ReadHolder currentReadHolder) {
ExcelReadHeadProperty excelReadHeadProperty = currentReadHolder.excelReadHeadProperty();
Object resultModel;
try {
@ -61,10 +61,10 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Integer index = entry.getKey();
if (index >= data.size()) {
if (index >= cellDataMap.size()) {
continue;
}
CellData cellData = data.get(index);
CellData cellData = cellDataMap.get(index);
if (cellData.getType() == CellDataTypeEnum.EMPTY) {
continue;
}
@ -81,6 +81,9 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty,
Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration) {
if (clazz == CellData.class) {
return cellData;
}
Converter converter = null;
if (contentProperty != null) {
converter = contentProperty.getConverter();

4
src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java

@ -1,6 +1,6 @@
package com.alibaba.excel.read.listener.event;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.metadata.CellData;
@ -16,5 +16,5 @@ public interface AnalysisFinishEvent {
*
* @return
*/
List<CellData> getAnalysisResult();
Map<Integer, CellData> getAnalysisResult();
}

9
src/main/java/com/alibaba/excel/read/listener/event/EachRowAnalysisFinishEvent.java

@ -1,6 +1,6 @@
package com.alibaba.excel.read.listener.event;
import java.util.List;
import java.util.Map;
import com.alibaba.excel.metadata.CellData;
@ -8,13 +8,14 @@ import com.alibaba.excel.metadata.CellData;
* @author jipengfei
*/
public class EachRowAnalysisFinishEvent implements AnalysisFinishEvent {
private List<CellData> result;
private Map<Integer, CellData> result;
public EachRowAnalysisFinishEvent(List<CellData> content) {
public EachRowAnalysisFinishEvent(Map<Integer, CellData> content) {
this.result = content;
}
@Override
public List<CellData> getAnalysisResult() {
public Map<Integer, CellData> getAnalysisResult() {
return result;
}
}

16
src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java

@ -107,9 +107,9 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
@Override
public void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext) {
List<CellData> cellDataList = event.getAnalysisResult();
Map<Integer, CellData> cellDataMap = event.getAnalysisResult();
ReadRowHolder readRowHolder = analysisContext.readRowHolder();
readRowHolder.setCurrentRowAnalysisResult(cellDataList);
readRowHolder.setCurrentRowAnalysisResult(cellDataMap);
if (readRowHolder.getRowIndex() >= analysisContext.readSheetHolder().getHeadRowNumber()) {
for (ReadListener readListener : analysisContext.currentReadHolder().readListenerList()) {
@ -129,7 +129,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
}
// Now is header
if (analysisContext.readSheetHolder().getHeadRowNumber().equals(readRowHolder.getRowIndex() + 1)) {
buildHead(analysisContext, cellDataList);
buildHead(analysisContext, cellDataMap);
}
}
@ -140,11 +140,11 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
}
}
private void buildHead(AnalysisContext analysisContext, List<CellData> cellDataList) {
private void buildHead(AnalysisContext analysisContext, Map<Integer, CellData> cellDataMap) {
if (!HeadKindEnum.CLASS.equals(analysisContext.currentReadHolder().excelReadHeadProperty().getHeadKind())) {
return;
}
List<String> dataList = (List<String>)buildStringList(cellDataList, analysisContext.currentReadHolder());
List<String> dataList = (List<String>)buildStringList(cellDataMap, analysisContext.currentReadHolder());
ExcelReadHeadProperty excelHeadPropertyData = analysisContext.readSheetHolder().excelReadHeadProperty();
Map<Integer, Head> headMapData = excelHeadPropertyData.getHeadMap();
Map<Integer, ExcelContentProperty> contentPropertyMapData = excelHeadPropertyData.getContentPropertyMap();
@ -153,7 +153,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
new HashMap<Integer, ExcelContentProperty>(contentPropertyMapData.size() * 4 / 3 + 1);
for (Map.Entry<Integer, Head> entry : headMapData.entrySet()) {
Head headData = entry.getValue();
if (headData.getForceIndex()) {
if (headData.getForceIndex() || !headData.getForceName()) {
tmpHeadMap.put(entry.getKey(), headData);
tmpContentPropertyMap.put(entry.getKey(), contentPropertyMapData.get(entry.getKey()));
continue;
@ -179,9 +179,9 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
excelHeadPropertyData.setContentPropertyMap(tmpContentPropertyMap);
}
private Object buildStringList(List<CellData> data, ReadHolder readHolder) {
private Object buildStringList(Map<Integer, CellData> cellDataMa, ReadHolder readHolder) {
List<String> list = new ArrayList<String>();
for (CellData cellData : data) {
for (CellData cellData : cellDataMa.values()) {
Converter converter =
readHolder.converterMap().get(ConverterKeyBuild.buildKey(String.class, cellData.getType()));
if (converter == null) {

47
src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -256,25 +256,7 @@ public class ExcelBuilderImpl implements ExcelBuilder {
if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
value = ((String)value).trim();
}
Converter converter = null;
if (excelContentProperty != null) {
converter = excelContentProperty.getConverter();
}
if (converter == null) {
converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
}
if (converter == null) {
throw new ExcelDataConvertException(
"Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
}
CellData cellData;
try {
cellData =
converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
} catch (Exception e) {
throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(),
e);
}
CellData cellData = convert(currentWriteHolder, clazz, cell, value, excelContentProperty);
if (cellData == null || cellData.getType() == null) {
throw new ExcelDataConvertException(
"Convert data:" + value + " return null,at row:" + cell.getRow().getRowNum());
@ -297,4 +279,31 @@ public class ExcelBuilderImpl implements ExcelBuilder {
+ "at row:" + cell.getRow().getRowNum());
}
}
private CellData convert(WriteHolder currentWriteHolder, Class clazz, Cell cell, Object value,
ExcelContentProperty excelContentProperty) {
if (value instanceof CellData) {
return (CellData)value;
}
Converter converter = null;
if (excelContentProperty != null) {
converter = excelContentProperty.getConverter();
}
if (converter == null) {
converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
}
if (converter == null) {
throw new ExcelDataConvertException(
"Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
}
CellData cellData;
try {
cellData =
converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
} catch (Exception e) {
throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(),
e);
}
return cellData;
}
}

3
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterData.java

@ -4,6 +4,7 @@ import java.math.BigDecimal;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.CellData;
import lombok.Data;
@ -32,4 +33,6 @@ public class ConverterData {
private Float floatData;
@ExcelProperty("字符串")
private String string;
@ExcelProperty("自定义")
private CellData cellData;
}

1
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java

@ -45,6 +45,7 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterData>
Assert.assertEquals(data.getDoubleData(), 1.0, 0.0);
Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0);
Assert.assertEquals(data.getString(), "测试");
Assert.assertEquals(data.getCellData().getStringValue(), "自定义");
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
}

29
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java

@ -5,12 +5,14 @@ import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.util.DateUtils;
/**
@ -21,8 +23,14 @@ import com.alibaba.excel.util.DateUtils;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ConverterDataTest {
private File file07 = TestFileUtil.createNewFile("converter07.xlsx");
private File file03 = TestFileUtil.createNewFile("converter03.xls");
private static File file07;
private static File file03;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("converter07.xlsx");
file03 = TestFileUtil.createNewFile("converter03.xls");
}
@Test
public void T01ReadAndWrite07() throws Exception {
@ -39,6 +47,22 @@ public class ConverterDataTest {
EasyExcelFactory.read(file, ConverterData.class, new ConverterDataListener()).sheet().doRead().finish();
}
@Test
public void T03ReadAllConverter07() {
readAllConverter("converter" + File.separator + "converter07.xlsx");
}
@Test
public void T03ReadAllConverter03() {
readAllConverter("converter" + File.separator + "converter03.xls");
}
private void readAllConverter(String fileName) {
EasyExcelFactory
.read(TestFileUtil.readFile(fileName), ReadAllConverterData.class, new ReadAllConverterDataListener())
.sheet().doRead().finish();
}
private List<ConverterData> data() throws Exception {
List<ConverterData> list = new ArrayList<ConverterData>();
ConverterData converterData = new ConverterData();
@ -52,6 +76,7 @@ public class ConverterDataTest {
converterData.setDoubleData(1.0);
converterData.setFloatData((float)1.0);
converterData.setString("测试");
converterData.setCellData(new CellData("自定义"));
list.add(converterData);
return list;
}

45
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java

@ -0,0 +1,45 @@
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.util.Date;
import lombok.Data;
/**
* @author zhuangjiaju
*/
@Data
public class ReadAllConverterData {
private BigDecimal bigDecimalBoolean;
private BigDecimal bigDecimalNumber;
private BigDecimal bigDecimalString;
private Boolean booleanBoolean;
private Boolean booleanNumber;
private Boolean booleanString;
private Byte byteBoolean;
private Byte byteNumber;
private Byte byteString;
private Date dateNumber;
private Date dateString;
private Double doubleBoolean;
private Double doubleNumber;
private Double doubleString;
private Float floatBoolean;
private Float floatNumber;
private Float floatString;
private Integer integerBoolean;
private Integer integerNumber;
private Integer integerString;
private Long longBoolean;
private Long longNumber;
private Long longString;
private Short shortBoolean;
private Short shortNumber;
private Short shortString;
private String StringBoolean;
private String StringNumber;
private String StringString;
private String StringError;
private String StringFormulaNumber;
private String StringFormulaString;
}

46
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java

@ -0,0 +1,46 @@
package com.alibaba.easyexcel.test.core.converter;
import java.util.ArrayList;
import java.util.List;
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.fastjson.JSON;
/**
* @author zhuangjiaju
*/
public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllConverterData> {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadAllConverterDataListener.class);
List<ReadAllConverterData> list = new ArrayList<ReadAllConverterData>();
@Override
public void invoke(ReadAllConverterData data, AnalysisContext context) {
list.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
Assert.assertEquals(list.size(), 1);
ReadAllConverterData data = list.get(0);
// try {
// Assert.assertEquals(data.getDate(), DateUtils.parseDate("2020-01-01 01:01:01"));
// } catch (ParseException e) {
// throw new ExcelCommonException("Test Exception", e);
// }
// Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
// Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
// Assert.assertEquals((long)data.getLongData(), 1L);
// Assert.assertEquals((long)data.getIntegerData(), 1L);
// Assert.assertEquals((long)data.getShortData(), 1L);
// Assert.assertEquals((long)data.getByteData(), 1L);
// Assert.assertEquals(data.getDoubleData(), 1.0, 0.0);
// Assert.assertEquals(data.getFloatData(), (float)1.0, 0.0);
// Assert.assertEquals(data.getString(), "测试");
LOGGER.debug("First row:{}", JSON.toJSONString(list.get(0)));
}
}

53
src/test/java/com/alibaba/easyexcel/test/core/converter/ReadConverterData.java

@ -1,53 +0,0 @@
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
/**
* @author zhuangjiaju
*/
@Data
public class ReadConverterData {
@ExcelProperty("日期")
private Date date;
@ExcelProperty("日期字符串")
private String dateString;
@ExcelProperty("布尔")
private Boolean booleanData;
@ExcelProperty("布尔字符串")
private String booleanString;
@ExcelProperty("大数")
private BigDecimal bigDecimal;
@ExcelProperty("大数字符串")
private String bigDecimalString;
@ExcelProperty("长整型")
private Long longData;
@ExcelProperty("长整型字符串")
private String longString;
@ExcelProperty("整型")
private Integer integerData;
@ExcelProperty("整型字符串")
private String integerString;
@ExcelProperty("短整型")
private Short shortData;
@ExcelProperty("短整型字符串")
private String shortString;
@ExcelProperty("字节型")
private Byte byteData;
@ExcelProperty("字节型字符串")
private String byteString;
@ExcelProperty("双精度浮点型")
private Double doulbleData;
@ExcelProperty("双精度浮点型字符串")
private String doulbleString;
@ExcelProperty("浮点型")
private Float FloatData;
@ExcelProperty("浮点型字符串")
private String FloatString;
@ExcelProperty("字符串")
private String string;
}

2
src/test/java/com/alibaba/easyexcel/test/core/nohead/NoHeadData07Test.java → src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadData07Test.java

@ -1,4 +1,4 @@
package com.alibaba.easyexcel.test.core.nohead;
package com.alibaba.easyexcel.test.core.head;
import java.util.ArrayList;
import java.util.List;

15
src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java

@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
@ -20,11 +21,17 @@ import com.alibaba.excel.EasyExcelFactory;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SimpleDataTest {
private File file07 = TestFileUtil.createNewFile("simple07.xlsx");
private File file03 = TestFileUtil.createNewFile("simple03.xls");
private static File file07;
private static File file03;
@BeforeClass
public static void init() {
file07 = TestFileUtil.createNewFile("simple07.xlsx");
file03 = TestFileUtil.createNewFile("simple03.xls");
}
@Test
public void T01ReadAndWrite() {
public void T01ReadAndWrite07() {
readAndWrite(file07);
}
@ -48,8 +55,6 @@ public class SimpleDataTest {
synchronousRead(file03);
}
private void synchronousRead(File file) {
// Synchronous read file
List<Object> list = EasyExcelFactory.read(file).head(SimpleData.class).sheet().doReadSync();

7
src/test/java/com/alibaba/easyexcel/test/util/TestFileUtil.java

@ -13,6 +13,10 @@ public class TestFileUtil {
return TestFileUtil.class.getResource("/").getPath();
}
public static File readFile(String pathName) {
return new File(getPath() + pathName);
}
public static File createNewFile(String pathName) {
File file = new File(getPath() + pathName);
if (file.exists()) {
@ -25,7 +29,4 @@ public class TestFileUtil {
return file;
}
public static File createNewWriteFile(String pathName) {
return createNewFile("wirte/" + pathName);
}
}

BIN
src/test/resources/converter/converter03.xls

Binary file not shown.

BIN
src/test/resources/converter/converter07.xlsx

Binary file not shown.
Loading…
Cancel
Save