Browse Source

优化读写的写法,方便读写

developing
zhuangjiaju 5 years ago
parent
commit
a9f12c078b
  1. 7
      pom.xml
  2. 2
      src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  3. 15
      src/main/java/com/alibaba/excel/analysis/v07/handlers/DefaultCellHandler.java
  4. 16
      src/main/java/com/alibaba/excel/annotation/ExcelProperty.java
  5. 36
      src/main/java/com/alibaba/excel/converters/AutoConverter.java
  6. 19
      src/main/java/com/alibaba/excel/converters/Converter.java
  7. 7
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java
  8. 7
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java
  9. 7
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java
  10. 7
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java
  11. 7
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java
  12. 7
      src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java
  13. 7
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java
  14. 7
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java
  15. 7
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java
  16. 15
      src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java
  17. 7
      src/main/java/com/alibaba/excel/converters/date/DateStringConverter.java
  18. 7
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleBooleanConverter.java
  19. 7
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java
  20. 7
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java
  21. 7
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatBooleanConverter.java
  22. 7
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java
  23. 7
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java
  24. 7
      src/main/java/com/alibaba/excel/converters/integer/IntegerBooleanConverter.java
  25. 7
      src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java
  26. 7
      src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java
  27. 7
      src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java
  28. 7
      src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java
  29. 7
      src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java
  30. 7
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java
  31. 7
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java
  32. 7
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java
  33. 7
      src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java
  34. 7
      src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java
  35. 7
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  36. 7
      src/main/java/com/alibaba/excel/converters/string/StringStringConverter.java
  37. 2
      src/main/java/com/alibaba/excel/metadata/AbstractHolder.java
  38. 36
      src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java
  39. 30
      src/main/java/com/alibaba/excel/metadata/Holder.java
  40. 21
      src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java
  41. 14
      src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java
  42. 3
      src/main/java/com/alibaba/excel/parameter/GenerateParam.java
  43. 22
      src/main/java/com/alibaba/excel/read/listener/ModelBuildEventListener.java
  44. 1
      src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java
  45. 11
      src/main/java/com/alibaba/excel/read/listener/event/AnalysisFinishEvent.java
  46. 2
      src/main/java/com/alibaba/excel/read/metadata/holder/AbstractReadHolder.java
  47. 12
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java
  48. 9
      src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java
  49. 2
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  50. 13
      src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  51. 14
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java

7
pom.xml

@ -56,13 +56,6 @@
</licenses> </licenses>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>

2
src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -83,6 +83,8 @@ public class XlsxSaxAnalyser implements ExcelExecutor {
CTWorkbookPr prefix = wb.getWorkbookPr(); CTWorkbookPr prefix = wb.getWorkbookPr();
if (prefix != null && prefix.getDate1904()) { if (prefix != null && prefix.getDate1904()) {
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE); readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.TRUE);
} else {
readWorkbookHolder.getGlobalConfiguration().setUse1904windowing(Boolean.FALSE);
} }
} }

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

@ -21,6 +21,7 @@ import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.util.BooleanUtils; import com.alibaba.excel.util.BooleanUtils;
import com.alibaba.excel.util.PositionUtils; import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
/** /**
* Cell Handler * Cell Handler
* *
@ -84,8 +85,12 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
ensureSize(); ensureSize();
// Have to go "sharedStrings.xml" and get it // Have to go "sharedStrings.xml" and get it
if (currentCellData.getType() == CellDataTypeEnum.STRING) { if (currentCellData.getType() == CellDataTypeEnum.STRING) {
currentCellData.setStringValue(analysisContext.readWorkbookHolder().getReadCache() String stringValue = analysisContext.readWorkbookHolder().getReadCache()
.get(Integer.valueOf(currentCellData.getStringValue()))); .get(Integer.valueOf(currentCellData.getStringValue()));
if (stringValue != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
stringValue = stringValue.trim();
}
currentCellData.setStringValue(stringValue);
} }
curRowContent[curCol] = currentCellData; curRowContent[curCol] = currentCellData;
} }
@ -93,7 +98,11 @@ public class DefaultCellHandler implements XlsxCellHandler, XlsxRowResultHolder
if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) { if (CELL_INLINE_STRING_VALUE_TAG.equals(name)) {
ensureSize(); ensureSize();
XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue()); XSSFRichTextString richTextString = new XSSFRichTextString(currentCellData.getStringValue());
currentCellData.setStringValue(richTextString.toString()); String stringValue = richTextString.toString();
if (stringValue != null && analysisContext.currentReadHolder().globalConfiguration().getAutoTrim()) {
stringValue = stringValue.trim();
}
currentCellData.setStringValue(stringValue);
curRowContent[curCol] = currentCellData; curRowContent[curCol] = currentCellData;
} }
} }

16
src/main/java/com/alibaba/excel/annotation/ExcelProperty.java

@ -6,6 +6,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.Converter;
/** /**
* @author jipengfei * @author jipengfei
*/ */
@ -16,10 +19,10 @@ public @interface ExcelProperty {
/** /**
* The name of the sheet header. * The name of the sheet header.
* *
* <li>write: It automatically merges when you have more than one head * <li>write: It automatically merges when you have more than one head
* <li>read: When you have multiple heads, take the first one * <li>read: When you have multiple heads, take the first one
* *
* @return * @return
*/ */
String[] value() default {""}; String[] value() default {""};
@ -28,11 +31,18 @@ public @interface ExcelProperty {
* Index of column * Index of column
* *
* Read or write it on the index of column,If it's equal to -1, it's sorted by Java class * Read or write it on the index of column,If it's equal to -1, it's sorted by Java class
* *
* @return * @return
*/ */
int index() default -1; int index() default -1;
/**
* Force the current field to use this converter.
*
* @return
*/
Class<? extends Converter> converter() default AutoConverter.class;
/** /**
* *
* default @see com.alibaba.excel.util.TypeUtil if default is not meet you can set format * default @see com.alibaba.excel.util.TypeUtil if default is not meet you can set format

36
src/main/java/com/alibaba/excel/converters/AutoConverter.java

@ -0,0 +1,36 @@
package com.alibaba.excel.converters;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
/**
* An empty converter.It's automatically converted by type.
*
* @author zhuangjiaju
*/
public class AutoConverter implements Converter {
@Override
public Class supportJavaTypeKey() {
return null;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return null;
}
@Override
public Object convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return null;
}
@Override
public CellData convertToExcelData(Object value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return null;
}
}

19
src/main/java/com/alibaba/excel/converters/Converter.java

@ -2,13 +2,14 @@ package com.alibaba.excel.converters;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.sun.istack.internal.NotNull; import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable; import com.sun.istack.internal.Nullable;
/** /**
* Convert between Java objects and excel objects * Convert between Java objects and excel objects
* *
* @author Dan Zheng * @author Dan Zheng
* @param <T> * @param <T>
*/ */
@ -16,35 +17,39 @@ public interface Converter<T> {
/** /**
* Back to object types in Java * Back to object types in Java
* *
* @return * @return
*/ */
Class supportJavaTypeKey(); Class supportJavaTypeKey();
/** /**
* Back to object enum in excel * Back to object enum in excel
* *
* @return * @return
*/ */
CellDataTypeEnum supportExcelTypeKey(); CellDataTypeEnum supportExcelTypeKey();
/** /**
* Convert excel objects to Java objects * Convert excel objects to Java objects
* *
* @param cellData * @param cellData
* @param contentProperty * @param contentProperty
* @param globalConfiguration
* @return * @return
* @throws Exception * @throws Exception
*/ */
T convertToJavaData(@NotNull CellData cellData, @Nullable ExcelContentProperty contentProperty) throws Exception; T convertToJavaData(@NotNull CellData cellData, @Nullable ExcelContentProperty contentProperty,
@NotNull GlobalConfiguration globalConfiguration) throws Exception;
/** /**
* Convert Java objects to excel objects * Convert Java objects to excel objects
* *
* @param value * @param value
* @param contentProperty * @param contentProperty
* @param globalConfiguration
* @return * @return
* @throws Exception * @throws Exception
*/ */
CellData convertToExcelData(@NotNull T value, @Nullable ExcelContentProperty contentProperty) throws Exception; CellData convertToExcelData(@NotNull T value, @Nullable ExcelContentProperty contentProperty,
@NotNull GlobalConfiguration globalConfiguration) throws Exception;
} }

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

@ -5,6 +5,7 @@ import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class BigDecimalBooleanConverter implements Converter<BigDecimal> {
} }
@Override @Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return BigDecimal.ONE; return BigDecimal.ONE;
} }
@ -33,7 +35,8 @@ public class BigDecimalBooleanConverter implements Converter<BigDecimal> {
} }
@Override @Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (BigDecimal.ONE.equals(value)) { if (BigDecimal.ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java

@ -5,6 +5,7 @@ import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,12 +26,14 @@ public class BigDecimalNumberConverter implements Converter<BigDecimal> {
} }
@Override @Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return BigDecimal.valueOf(cellData.getDoubleValue()); return BigDecimal.valueOf(cellData.getDoubleValue());
} }
@Override @Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }
} }

7
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java

@ -5,6 +5,7 @@ import java.math.BigDecimal;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -26,12 +27,14 @@ public class BigDecimalStringConverter implements Converter<BigDecimal> {
} }
@Override @Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new BigDecimal(cellData.getStringValue()); return new BigDecimal(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

7
src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.booleanconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class BooleanBooleanConverter implements Converter<Boolean> {
} }
@Override @Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getBooleanValue(); return cellData.getBooleanValue();
} }
@Override @Override
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value); return new CellData(value);
} }

7
src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.booleanconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -26,7 +27,8 @@ public class BooleanNumberConverter implements Converter<Boolean> {
} }
@Override @Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(cellData.getDoubleValue())) { if (ONE.equals(cellData.getDoubleValue())) {
return Boolean.TRUE; return Boolean.TRUE;
} }
@ -34,7 +36,8 @@ public class BooleanNumberConverter implements Converter<Boolean> {
} }
@Override @Override
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (value) { if (value) {
return new CellData(ONE); return new CellData(ONE);
} }

7
src/main/java/com/alibaba/excel/converters/booleanconverter/BooleanStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.booleanconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class BooleanStringConverter implements Converter<Boolean> {
} }
@Override @Override
public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Boolean convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Boolean.valueOf(cellData.getStringValue()); return Boolean.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Boolean value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.toString()); return new CellData(value.toString());
} }

7
src/main/java/com/alibaba/excel/converters/byteconverter/ByteBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.byteconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class ByteBooleanConverter implements Converter<Byte> {
} }
@Override @Override
public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class ByteBooleanConverter implements Converter<Byte> {
} }
@Override @Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.byteconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class ByteNumberConverter implements Converter<Byte> {
} }
@Override @Override
public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().byteValue(); return cellData.getDoubleValue().byteValue();
} }
@Override @Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }

7
src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.byteconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class ByteStringConverter implements Converter<Byte> {
} }
@Override @Override
public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Byte.valueOf(cellData.getStringValue()); return Byte.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }

15
src/main/java/com/alibaba/excel/converters/date/DateNumberConverter.java

@ -7,11 +7,12 @@ import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
* Date and number converter * Date and number converter
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class DateNumberConverter implements Converter<Date> { public class DateNumberConverter implements Converter<Date> {
@ -27,13 +28,10 @@ public class DateNumberConverter implements Converter<Date> {
} }
@Override @Override
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
Boolean use1904windowing = Boolean.FALSE; return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), globalConfiguration.getUse1904windowing(), null);
if (contentProperty != null && contentProperty.getUse1904windowing() != null) {
use1904windowing = contentProperty.getUse1904windowing();
}
return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), use1904windowing, null);
} else { } else {
return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), return HSSFDateUtil.getJavaDate(cellData.getDoubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null); contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null);
@ -41,7 +39,8 @@ public class DateNumberConverter implements Converter<Date> {
} }
@Override @Override
public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData((double)(value.getTime())); return new CellData((double)(value.getTime()));
} }
} }

7
src/main/java/com/alibaba/excel/converters/date/DateStringConverter.java

@ -6,6 +6,7 @@ import java.util.Date;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
@ -26,7 +27,8 @@ public class DateStringConverter implements Converter<Date> {
} }
@Override @Override
public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) throws ParseException { public Date convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws ParseException {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseDate(cellData.getStringValue(), null); return DateUtils.parseDate(cellData.getStringValue(), null);
} else { } else {
@ -36,7 +38,8 @@ public class DateStringConverter implements Converter<Date> {
} }
@Override @Override
public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Date value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new CellData(DateUtils.format(value, null)); return new CellData(DateUtils.format(value, null));
} else { } else {

7
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.doubleconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class DoubleBooleanConverter implements Converter<Double> {
} }
@Override @Override
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class DoubleBooleanConverter implements Converter<Double> {
} }
@Override @Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.doubleconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class DoubleNumberConverter implements Converter<Double> {
} }
@Override @Override
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue(); return cellData.getDoubleValue();
} }
@Override @Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value); return new CellData(value);
} }

7
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.doubleconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class DoubleStringConverter implements Converter<Double> {
} }
@Override @Override
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Double.valueOf(cellData.getStringValue()); return Double.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

7
src/main/java/com/alibaba/excel/converters/floatconverter/FloatBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.floatconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class FloatBooleanConverter implements Converter<Float> {
} }
@Override @Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class FloatBooleanConverter implements Converter<Float> {
} }
@Override @Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.floatconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class FloatNumberConverter implements Converter<Float> {
} }
@Override @Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().floatValue(); return cellData.getDoubleValue().floatValue();
} }
@Override @Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }

7
src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.floatconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class FloatStringConverter implements Converter<Float> {
} }
@Override @Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Float.valueOf(cellData.getStringValue()); return Float.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

7
src/main/java/com/alibaba/excel/converters/integer/IntegerBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.integer;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class IntegerBooleanConverter implements Converter<Integer> {
} }
@Override @Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class IntegerBooleanConverter implements Converter<Integer> {
} }
@Override @Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.integer;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class IntegerNumberConverter implements Converter<Integer> {
} }
@Override @Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().intValue(); return cellData.getDoubleValue().intValue();
} }
@Override @Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }

7
src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.integer;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class IntegerStringConverter implements Converter<Integer> {
} }
@Override @Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Integer.valueOf(cellData.getStringValue()); return Integer.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

7
src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.longconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class LongBooleanConverter implements Converter<Long> {
} }
@Override @Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class LongBooleanConverter implements Converter<Long> {
} }
@Override @Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.longconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class LongNumberConverter implements Converter<Long> {
} }
@Override @Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().longValue(); return cellData.getDoubleValue().longValue();
} }
@Override @Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }

7
src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.longconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class LongStringConverter implements Converter<Long> {
} }
@Override @Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Long.valueOf(cellData.getStringValue()); return Long.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

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

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.shortconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -25,7 +26,8 @@ public class ShortBooleanConverter implements Converter<Short> {
} }
@Override @Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +35,8 @@ public class ShortBooleanConverter implements Converter<Short> {
} }
@Override @Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

7
src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.shortconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class ShortNumberConverter implements Converter<Short> {
} }
@Override @Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getDoubleValue().shortValue(); return cellData.getDoubleValue().shortValue();
} }
@Override @Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value.doubleValue()); return new CellData(value.doubleValue());
} }

7
src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.shortconverter;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -24,12 +25,14 @@ public class ShortStringConverter implements Converter<Short> {
} }
@Override @Override
public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return Short.valueOf(cellData.getStringValue()); return Short.valueOf(cellData.getStringValue());
} }
@Override @Override
public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty); return NumberUtils.formatToCellData(value, contentProperty);
} }
} }

7
src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.string;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -23,12 +24,14 @@ public class StringBooleanConverter implements Converter<String> {
} }
@Override @Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getBooleanValue().toString(); return cellData.getBooleanValue().toString();
} }
@Override @Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(Boolean.valueOf(value)); return new CellData(Boolean.valueOf(value));
} }

7
src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.string;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -22,12 +23,14 @@ public class StringErrorConverter implements Converter<String> {
} }
@Override @Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getStringValue(); return cellData.getStringValue();
} }
@Override @Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(CellDataTypeEnum.ERROR, value); return new CellData(CellDataTypeEnum.ERROR, value);
} }

7
src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java

@ -5,6 +5,7 @@ import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
@ -27,7 +28,8 @@ public class StringNumberConverter implements Converter<String> {
} }
@Override @Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
// If there are "DateTimeFormat", read as date // If there are "DateTimeFormat", read as date
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) { if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
return DateUtils.format( return DateUtils.format(
@ -40,7 +42,8 @@ public class StringNumberConverter implements Converter<String> {
} }
@Override @Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(Double.valueOf(value)); return new CellData(Double.valueOf(value));
} }
} }

7
src/main/java/com/alibaba/excel/converters/string/StringStringConverter.java

@ -3,6 +3,7 @@ package com.alibaba.excel.converters.string;
import com.alibaba.excel.converters.Converter; import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum; import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
@ -22,12 +23,14 @@ public class StringStringConverter implements Converter<String> {
} }
@Override @Override
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getStringValue(); return cellData.getStringValue();
} }
@Override @Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(value); return new CellData(value);
} }

2
src/main/java/com/alibaba/excel/metadata/AbstractHolder.java

@ -10,7 +10,7 @@ import com.alibaba.excel.converters.Converter;
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public abstract class AbstractHolder implements Holder { public abstract class AbstractHolder implements ConfigurationHolder {
/** /**
* Record whether it's new or from cache * Record whether it's new or from cache
*/ */

36
src/main/java/com/alibaba/excel/metadata/ConfigurationHolder.java

@ -0,0 +1,36 @@
package com.alibaba.excel.metadata;
import java.util.Map;
import com.alibaba.excel.converters.Converter;
/**
*
* Get the corresponding holder
*
* @author zhuangjiaju
**/
public interface ConfigurationHolder extends Holder {
/**
*
* Record whether it's new or from cache
*
* @return
*/
boolean isNew();
/**
* Some global variables
*
* @return
*/
GlobalConfiguration globalConfiguration();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<String, Converter> converterMap();
}

30
src/main/java/com/alibaba/excel/metadata/Holder.java

@ -1,44 +1,20 @@
package com.alibaba.excel.metadata; package com.alibaba.excel.metadata;
import java.util.Map;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.HolderEnum; import com.alibaba.excel.enums.HolderEnum;
/** /**
* *
* Get the corresponding holder * Get the corresponding holder
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
public interface Holder { public interface Holder {
/** /**
* What holder is the return * What holder is the return
* *
* @return * @return
*/ */
HolderEnum holderType(); HolderEnum holderType();
/**
*
* Record whether it's new or from cache
*
* @return
*/
boolean isNew();
/**
* Some global variables
*
* @return
*/
GlobalConfiguration globalConfiguration();
/**
* What converter does the currently operated cell need to execute
*
* @return
*/
Map<String, Converter> converterMap();
} }

21
src/main/java/com/alibaba/excel/metadata/property/ExcelContentProperty.java

@ -2,6 +2,7 @@ package com.alibaba.excel.metadata.property;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
/** /**
@ -16,17 +17,13 @@ public class ExcelContentProperty {
* Excel head * Excel head
*/ */
private Head head; private Head head;
/**
* Custom defined converters
*/
private Converter converter;
private CellStyleProperty cellStyleProperty; private CellStyleProperty cellStyleProperty;
private DateTimeFormatProperty dateTimeFormatProperty; private DateTimeFormatProperty dateTimeFormatProperty;
private NumberFormatProperty numberFormatProperty; private NumberFormatProperty numberFormatProperty;
/**
* true if date uses 1904 windowing, or false if using 1900 date windowing.
*
* default is false
*
* @return
*/
private Boolean use1904windowing;
public Field getField() { public Field getField() {
return field; return field;
@ -68,11 +65,11 @@ public class ExcelContentProperty {
this.numberFormatProperty = numberFormatProperty; this.numberFormatProperty = numberFormatProperty;
} }
public Boolean getUse1904windowing() { public Converter getConverter() {
return use1904windowing; return converter;
} }
public void setUse1904windowing(Boolean use1904windowing) { public void setConverter(Converter converter) {
this.use1904windowing = use1904windowing; this.converter = converter;
} }
} }

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

@ -14,7 +14,10 @@ import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat; import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat; import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.HeadKindEnum; import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.exception.ExcelGenerateException; import com.alibaba.excel.exception.ExcelGenerateException;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StringUtils; import com.alibaba.excel.util.StringUtils;
@ -162,6 +165,17 @@ public class ExcelHeadProperty {
} }
Head head = new Head(index, field.getName(), tmpHeadList, forceIndex); Head head = new Head(index, field.getName(), tmpHeadList, forceIndex);
ExcelContentProperty excelContentProperty = new ExcelContentProperty(); ExcelContentProperty excelContentProperty = new ExcelContentProperty();
if (excelProperty != null && excelProperty.converter() != null) {
Class<? extends Converter> convertClazz = excelProperty.converter();
if (convertClazz != AutoConverter.class) {
try {
Converter converter = convertClazz.newInstance();
excelContentProperty.setConverter(converter);
} catch (Exception e) {
throw new ExcelCommonException("Can not instance custom converter:" + convertClazz.getName());
}
}
}
excelContentProperty.setHead(head); excelContentProperty.setHead(head);
excelContentProperty.setField(field); excelContentProperty.setField(field);
excelContentProperty excelContentProperty

3
src/main/java/com/alibaba/excel/parameter/GenerateParam.java

@ -6,7 +6,8 @@ import com.alibaba.excel.support.ExcelTypeEnum;
/** /**
* Created by jipengfei on 17/2/19. * Created by jipengfei on 17/2/19.
* *
* @author jipengfei
* @deprecated please use {@link com.alibaba.excel.write.builder.ExcelWriterBuilder} build ExcelWriter * @deprecated please use {@link com.alibaba.excel.write.builder.ExcelWriterBuilder} build ExcelWriter
*/ */
@Deprecated @Deprecated

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

@ -13,6 +13,7 @@ import com.alibaba.excel.enums.HeadKindEnum;
import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener; import com.alibaba.excel.event.AbstractIgnoreExceptionReadListener;
import com.alibaba.excel.exception.ExcelDataConvertException; import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.Head; import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.property.ExcelContentProperty; import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.read.metadata.holder.ReadHolder; import com.alibaba.excel.read.metadata.holder.ReadHolder;
@ -22,7 +23,7 @@ import net.sf.cglib.beans.BeanMap;
/** /**
* Convert to the object the user needs * Convert to the object the user needs
* *
* @author jipengfei * @author jipengfei
*/ */
public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<List<CellData>> { public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener<List<CellData>> {
@ -40,7 +41,8 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
private Object buildStringList(List<CellData> data, ReadHolder currentReadHolder) { private Object buildStringList(List<CellData> data, ReadHolder currentReadHolder) {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
for (CellData cellData : data) { for (CellData cellData : data) {
list.add((String)convertValue(cellData, String.class, null, currentReadHolder.converterMap())); list.add((String)convertValue(cellData, String.class, null, currentReadHolder.converterMap(),
currentReadHolder.globalConfiguration()));
} }
return list; return list;
} }
@ -54,8 +56,8 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e); "Can not instance class: " + excelReadHeadProperty.getHeadClazz().getName(), e);
} }
Map<String, Object> map = new HashMap<String, Object>();
Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap(); Map<Integer, Head> headMap = excelReadHeadProperty.getHeadMap();
Map<String, Object> map = new HashMap<String, Object>(headMap.size() * 4 / 3 + 1);
Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap(); Map<Integer, ExcelContentProperty> contentPropertyMap = excelReadHeadProperty.getContentPropertyMap();
for (Map.Entry<Integer, Head> entry : headMap.entrySet()) { for (Map.Entry<Integer, Head> entry : headMap.entrySet()) {
Integer index = entry.getKey(); Integer index = entry.getKey();
@ -68,7 +70,7 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
} }
ExcelContentProperty excelContentProperty = contentPropertyMap.get(index); ExcelContentProperty excelContentProperty = contentPropertyMap.get(index);
Object value = convertValue(cellData, excelContentProperty.getField().getType(), excelContentProperty, Object value = convertValue(cellData, excelContentProperty.getField().getType(), excelContentProperty,
currentReadHolder.converterMap()); currentReadHolder.converterMap(), currentReadHolder.globalConfiguration());
if (value != null) { if (value != null) {
map.put(excelContentProperty.getField().getName(), value); map.put(excelContentProperty.getField().getName(), value);
} }
@ -78,14 +80,20 @@ public class ModelBuildEventListener extends AbstractIgnoreExceptionReadListener
} }
private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty, private Object convertValue(CellData cellData, Class clazz, ExcelContentProperty contentProperty,
Map<String, Converter> converterMap) { Map<String, Converter> converterMap, GlobalConfiguration globalConfiguration) {
Converter converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType())); Converter converter = null;
if (contentProperty != null) {
converter = contentProperty.getConverter();
}
if (converter == null) {
converter = converterMap.get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
}
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Converter not found, convert " + cellData.getType() + " to " + clazz.getName()); "Converter not found, convert " + cellData.getType() + " to " + clazz.getName());
} }
try { try {
return converter.convertToJavaData(cellData, contentProperty); return converter.convertToJavaData(cellData, contentProperty, globalConfiguration);
} catch (Exception e) { } catch (Exception e) {
throw new ExcelDataConvertException("Convert data " + cellData + " to " + clazz + " error ", e); throw new ExcelDataConvertException("Convert data " + cellData + " to " + clazz + " error ", e);
} }

1
src/main/java/com/alibaba/excel/read/listener/ReadListenerRegistryCenter.java

@ -28,6 +28,7 @@ public interface ReadListenerRegistryCenter {
void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext); void notifyEndOneRow(AnalysisFinishEvent event, AnalysisContext analysisContext);
/** /**
* Notify after all analysed
* *
* @param analysisContext * @param analysisContext
*/ */

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

@ -1,5 +1,16 @@
package com.alibaba.excel.read.listener.event; package com.alibaba.excel.read.listener.event;
/**
*
* Event
*
* @author jipengfei
*/
public interface AnalysisFinishEvent { public interface AnalysisFinishEvent {
/**
* Get result
*
* @return
*/
Object getAnalysisResult(); Object getAnalysisResult();
} }

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

@ -189,7 +189,7 @@ public abstract class AbstractReadHolder extends AbstractHolder implements ReadH
"Converter not found, convert " + cellData.getType() + " to String"); "Converter not found, convert " + cellData.getType() + " to String");
} }
try { try {
list.add((String)(converter.convertToJavaData(cellData, null))); list.add((String)(converter.convertToJavaData(cellData, null, readHolder.globalConfiguration())));
} catch (Exception e) { } catch (Exception e) {
throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e); throw new ExcelDataConvertException("Convert data " + cellData + " to String error ", e);
} }

12
src/main/java/com/alibaba/excel/read/metadata/holder/ReadHolder.java

@ -2,27 +2,27 @@ package com.alibaba.excel.read.metadata.holder;
import java.util.List; import java.util.List;
import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.ConfigurationHolder;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty; import com.alibaba.excel.read.metadata.property.ExcelReadHeadProperty;
/** /**
* *
* Get the corresponding Holder * Get the corresponding Holder
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
public interface ReadHolder extends Holder { public interface ReadHolder extends ConfigurationHolder {
/** /**
* What handler does the currently operated cell need to execute * What handler does the currently operated cell need to execute
* *
* @return * @return
*/ */
List<ReadListener> readListenerList(); List<ReadListener> readListenerList();
/** /**
* What 'ExcelReadHeadProperty' does the currently operated cell need to execute * What 'ExcelReadHeadProperty' does the currently operated cell need to execute
* *
* @return * @return
*/ */
ExcelReadHeadProperty excelReadHeadProperty(); ExcelReadHeadProperty excelReadHeadProperty();

9
src/main/java/com/alibaba/excel/read/metadata/holder/ReadRowHolder.java

@ -1,13 +1,15 @@
package com.alibaba.excel.read.metadata.holder; package com.alibaba.excel.read.metadata.holder;
import com.alibaba.excel.enums.HolderEnum;
import com.alibaba.excel.metadata.GlobalConfiguration; import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.Holder;
/** /**
* sheet holder * sheet holder
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class ReadRowHolder { public class ReadRowHolder implements Holder {
/** /**
* Returns row index of a row in the sheet that contains this cell.Start form 0. * Returns row index of a row in the sheet that contains this cell.Start form 0.
*/ */
@ -50,4 +52,9 @@ public class ReadRowHolder {
public void setRowIndex(int rowIndex) { public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex; this.rowIndex = rowIndex;
} }
@Override
public HolderEnum holderType() {
return HolderEnum.ROW;
}
} }

2
src/main/java/com/alibaba/excel/util/WorkBookUtil.java

@ -17,7 +17,7 @@ import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
/** /**
* *
* @author jipengfei * @author jipengfei
*/ */
public class WorkBookUtil { public class WorkBookUtil {

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

@ -76,7 +76,6 @@ public class ExcelBuilderImpl implements ExcelBuilder {
@Override @Override
public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) { public void addContent(List data, WriteSheet writeSheet, WriteTable writeTable) {
try { try {
context.currentSheet(writeSheet); context.currentSheet(writeSheet);
context.currentTable(writeTable); context.currentTable(writeTable);
doAddContent(data); doAddContent(data);
@ -254,15 +253,21 @@ public class ExcelBuilderImpl implements ExcelBuilder {
if (value == null) { if (value == null) {
return; return;
} }
Converter converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz)); if (value instanceof String && currentWriteHolder.globalConfiguration().getAutoTrim()) {
value = ((String)value).trim();
}
Converter converter = excelContentProperty.getConverter();
if (converter == null) {
converter = currentWriteHolder.converterMap().get(ConverterKeyBuild.buildKey(clazz));
}
if (converter == null) { if (converter == null) {
throw new ExcelDataConvertException( throw new ExcelDataConvertException(
"Can not find 'Converter' support class " + clazz.getSimpleName() + "."); "Can not find 'Converter' support class " + clazz.getSimpleName() + ".");
} }
CellData cellData; CellData cellData;
try { try {
cellData = converter.convertToExcelData(value, excelContentProperty); cellData =
converter.convertToExcelData(value, excelContentProperty, currentWriteHolder.globalConfiguration());
} catch (Exception e) { } catch (Exception e) {
throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(), throw new ExcelDataConvertException("Convert data:" + value + " error,at row:" + cell.getRow().getRowNum(),
e); e);

14
src/main/java/com/alibaba/excel/write/metadata/holder/WriteHolder.java

@ -3,17 +3,17 @@ package com.alibaba.excel.write.metadata.holder;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.metadata.Holder; import com.alibaba.excel.metadata.ConfigurationHolder;
import com.alibaba.excel.write.handler.WriteHandler; import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.property.ExcelWriteHeadProperty; import com.alibaba.excel.write.property.ExcelWriteHeadProperty;
/** /**
* *
* Get the corresponding Holder * Get the corresponding Holder
* *
* @author zhuangjiaju * @author zhuangjiaju
**/ **/
public interface WriteHolder extends Holder { public interface WriteHolder extends ConfigurationHolder {
/** /**
* What 'ExcelWriteHeadProperty' does the currently operated cell need to execute * What 'ExcelWriteHeadProperty' does the currently operated cell need to execute
*/ */
@ -21,21 +21,21 @@ public interface WriteHolder extends Holder {
/** /**
* What handler does the currently operated cell need to execute * What handler does the currently operated cell need to execute
* *
* @return * @return
*/ */
Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap(); Map<Class<? extends WriteHandler>, List<WriteHandler>> writeHandlerMap();
/** /**
* Whether a header is required for the currently operated cell * Whether a header is required for the currently operated cell
* *
* @return * @return
*/ */
boolean needHead(); boolean needHead();
/** /**
* Writes the head relative to the existing contents of the sheet. Indexes are zero-based. * Writes the head relative to the existing contents of the sheet. Indexes are zero-based.
* *
* @return * @return
*/ */
int relativeHeadRowIndex(); int relativeHeadRowIndex();

Loading…
Cancel
Save