Browse Source

新增localdatetime

pull/2077/head
Jiaju Zhuang 3 years ago
parent
commit
3bf0158951
  1. 16
      src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  2. 47
      src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerBooleanConverter.java
  3. 41
      src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerNumberConverter.java
  4. 55
      src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java
  5. 34
      src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java
  6. 52
      src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeStringConverter.java
  7. 14
      src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java
  8. 45
      src/main/java/com/alibaba/excel/util/DateUtils.java
  9. 3
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  10. 3
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  11. 6
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java
  12. 6
      src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java
  13. 7
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java
  14. 6
      src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  15. BIN
      src/test/resources/converter/converter03.xls
  16. BIN
      src/test/resources/converter/converter07.xlsx
  17. 4
      src/test/resources/converter/converterCsv.csv

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

@ -5,6 +5,8 @@ import java.util.Map;
import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter; import com.alibaba.excel.converters.bigdecimal.BigDecimalStringConverter;
import com.alibaba.excel.converters.biginteger.BigIntegerBooleanConverter;
import com.alibaba.excel.converters.biginteger.BigIntegerNumberConverter;
import com.alibaba.excel.converters.biginteger.BigIntegerStringConverter; import com.alibaba.excel.converters.biginteger.BigIntegerStringConverter;
import com.alibaba.excel.converters.booleanconverter.BooleanBooleanConverter; import com.alibaba.excel.converters.booleanconverter.BooleanBooleanConverter;
import com.alibaba.excel.converters.booleanconverter.BooleanNumberConverter; import com.alibaba.excel.converters.booleanconverter.BooleanNumberConverter;
@ -28,6 +30,9 @@ import com.alibaba.excel.converters.inputstream.InputStreamImageConverter;
import com.alibaba.excel.converters.integer.IntegerBooleanConverter; import com.alibaba.excel.converters.integer.IntegerBooleanConverter;
import com.alibaba.excel.converters.integer.IntegerNumberConverter; import com.alibaba.excel.converters.integer.IntegerNumberConverter;
import com.alibaba.excel.converters.integer.IntegerStringConverter; import com.alibaba.excel.converters.integer.IntegerStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateNumberConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter;
import com.alibaba.excel.converters.longconverter.LongBooleanConverter; import com.alibaba.excel.converters.longconverter.LongBooleanConverter;
import com.alibaba.excel.converters.longconverter.LongNumberConverter; import com.alibaba.excel.converters.longconverter.LongNumberConverter;
import com.alibaba.excel.converters.longconverter.LongStringConverter; import com.alibaba.excel.converters.longconverter.LongStringConverter;
@ -61,6 +66,10 @@ public class DefaultConverterLoader {
putAllConverter(new BigDecimalNumberConverter()); putAllConverter(new BigDecimalNumberConverter());
putAllConverter(new BigDecimalStringConverter()); putAllConverter(new BigDecimalStringConverter());
putAllConverter(new BigIntegerBooleanConverter());
putAllConverter(new BigIntegerNumberConverter());
putAllConverter(new BigIntegerStringConverter());
putAllConverter(new BooleanBooleanConverter()); putAllConverter(new BooleanBooleanConverter());
putAllConverter(new BooleanNumberConverter()); putAllConverter(new BooleanNumberConverter());
putAllConverter(new BooleanStringConverter()); putAllConverter(new BooleanStringConverter());
@ -72,6 +81,9 @@ public class DefaultConverterLoader {
putAllConverter(new DateNumberConverter()); putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter()); putAllConverter(new DateStringConverter());
putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateTimeStringConverter());
putAllConverter(new DoubleBooleanConverter()); putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter()); putAllConverter(new DoubleNumberConverter());
putAllConverter(new DoubleStringConverter()); putAllConverter(new DoubleStringConverter());
@ -103,9 +115,11 @@ public class DefaultConverterLoader {
private static void initDefaultWriteConverter() { private static void initDefaultWriteConverter() {
defaultWriteConverter = MapUtils.newHashMapWithExpectedSize(40); defaultWriteConverter = MapUtils.newHashMapWithExpectedSize(40);
putWriteConverter(new BigDecimalNumberConverter()); putWriteConverter(new BigDecimalNumberConverter());
putWriteConverter(new BigIntegerNumberConverter());
putWriteConverter(new BooleanBooleanConverter()); putWriteConverter(new BooleanBooleanConverter());
putWriteConverter(new ByteNumberConverter()); putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter()); putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new DoubleNumberConverter()); putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter()); putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter()); putWriteConverter(new IntegerNumberConverter());
@ -120,9 +134,11 @@ public class DefaultConverterLoader {
// In some cases, it must be converted to string // In some cases, it must be converted to string
putWriteStringConverter(new BigDecimalStringConverter()); putWriteStringConverter(new BigDecimalStringConverter());
putWriteStringConverter(new BigIntegerStringConverter());
putWriteStringConverter(new BooleanStringConverter()); putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter()); putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter()); putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter()); putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter()); putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter()); putWriteStringConverter(new IntegerStringConverter());

47
src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerBooleanConverter.java

@ -0,0 +1,47 @@
package com.alibaba.excel.converters.biginteger;
import java.math.BigInteger;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
/**
* BigInteger and boolean converter
*
* @author Jiaju Zhuang
*/
public class BigIntegerBooleanConverter implements Converter<BigInteger> {
@Override
public Class<BigInteger> supportJavaTypeKey() {
return BigInteger.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.BOOLEAN;
}
@Override
public BigInteger convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (cellData.getBooleanValue()) {
return BigInteger.ONE;
}
return BigInteger.ZERO;
}
@Override
public WriteCellData<?> convertToExcelData(BigInteger value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (BigInteger.ONE.equals(value)) {
return new WriteCellData<>(Boolean.TRUE);
}
return new WriteCellData<>(Boolean.FALSE);
}
}

41
src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerNumberConverter.java

@ -0,0 +1,41 @@
package com.alibaba.excel.converters.biginteger;
import java.math.BigInteger;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
/**
* BigInteger and number converter
*
* @author Jiaju Zhuang
*/
public class BigIntegerNumberConverter implements Converter<BigInteger> {
@Override
public Class<BigInteger> supportJavaTypeKey() {
return BigInteger.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public BigInteger convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return cellData.getNumberValue().toBigInteger();
}
@Override
public WriteCellData<?> convertToExcelData(BigInteger value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return NumberUtils.formatToCellData(value, contentProperty);
}
}

55
src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java

@ -0,0 +1,55 @@
package com.alibaba.excel.converters.localdatetime;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* LocalDateTime and number converter
*
* @author Jiaju Zhuang
*/
public class LocalDateNumberConverter implements Converter<LocalDateTime> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtil.getLocalDateTime(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}
@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
} else {
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
}
}
}

34
src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java

@ -0,0 +1,34 @@
package com.alibaba.excel.converters.localdatetime;
import java.time.LocalDateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.WorkBookUtil;
/**
* Date and date converter
*
* @author Jiaju Zhuang
*/
public class LocalDateTimeDateConverter implements Converter<LocalDateTime> {
@Override
public Class<LocalDateTime> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws Exception {
WriteCellData<?> cellData = new WriteCellData<>(value);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultDateFormat);
return cellData;
}
}

52
src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeStringConverter.java

@ -0,0 +1,52 @@
package com.alibaba.excel.converters.localdatetime;
import java.text.ParseException;
import java.time.LocalDateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
/**
* LocalDateTime and string converter
*
* @author Jiaju Zhuang
*/
public class LocalDateTimeStringConverter implements Converter<LocalDateTime> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDateTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws ParseException {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseLocalDateTime(cellData.getStringValue(), null, globalConfiguration.getLocale());
} else {
return DateUtils.parseLocalDateTime(cellData.getStringValue(),
contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale());
}
}
@Override
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
} else {
return new WriteCellData<>(
DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat(),
globalConfiguration.getLocale()));
}
}
}

14
src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java

@ -1,6 +1,8 @@
package com.alibaba.excel.metadata.data; package com.alibaba.excel.metadata.data;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -22,7 +24,8 @@ public class WriteCellData<T> extends CellData<T> {
/** /**
* Support only when writing.{@link CellDataTypeEnum#DATE} * Support only when writing.{@link CellDataTypeEnum#DATE}
*/ */
private Date dateValue; private LocalDateTime dateValue;
/** /**
* rich text.{@link CellDataTypeEnum#RICH_TEXT_STRING} * rich text.{@link CellDataTypeEnum#RICH_TEXT_STRING}
*/ */
@ -84,6 +87,15 @@ public class WriteCellData<T> extends CellData<T> {
} }
public WriteCellData(Date dateValue) { public WriteCellData(Date dateValue) {
super();
if (dateValue == null) {
throw new IllegalArgumentException("DateValue can not be null");
}
setType(CellDataTypeEnum.DATE);
this.dateValue = LocalDateTime.ofInstant(dateValue.toInstant(), ZoneId.systemDefault());
}
public WriteCellData(LocalDateTime dateValue) {
super(); super();
if (dateValue == null) { if (dateValue == null) {
throw new IllegalArgumentException("DateValue can not be null"); throw new IllegalArgumentException("DateValue can not be null");

45
src/main/java/com/alibaba/excel/util/DateUtils.java

@ -3,8 +3,11 @@ package com.alibaba.excel.util;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -69,6 +72,25 @@ public class DateUtils {
return getCacheDateFormat(dateFormat).parse(dateString); return getCacheDateFormat(dateFormat).parse(dateString);
} }
/**
* convert string to date
*
* @param dateString
* @param dateFormat
* @param local
* @return
*/
public static LocalDateTime parseLocalDateTime(String dateString, String dateFormat, Locale local) {
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = switchDateFormat(dateString);
}
if (local == null) {
return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));
} else {
return LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local));
}
}
/** /**
* convert string to date * convert string to date
* *
@ -86,7 +108,7 @@ public class DateUtils {
* @param dateString * @param dateString
* @return * @return
*/ */
private static String switchDateFormat(String dateString) { public static String switchDateFormat(String dateString) {
int length = dateString.length(); int length = dateString.length();
switch (length) { switch (length) {
case 19: case 19:
@ -141,6 +163,27 @@ public class DateUtils {
return getCacheDateFormat(dateFormat).format(date); return getCacheDateFormat(dateFormat).format(date);
} }
/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDateTime date, String dateFormat, Locale local) {
if (date == null) {
return null;
}
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = defaultDateFormat;
}
if (local == null) {
return date.format(DateTimeFormatter.ofPattern(dateFormat));
} else {
return date.format(DateTimeFormatter.ofPattern(dateFormat, local));
}
}
private static DateFormat getCacheDateFormat(String dateFormat) { private static DateFormat getCacheDateFormat(String dateFormat) {
Map<String, SimpleDateFormat> dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get(); Map<String, SimpleDateFormat> dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get();
if (dateFormatMap == null) { if (dateFormatMap == null) {

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

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.core.converter; package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -36,8 +37,10 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterReadDa
} catch (ParseException e) { } catch (ParseException e) {
throw new ExcelCommonException("Test Exception", e); throw new ExcelCommonException("Test Exception", e);
} }
Assert.assertEquals(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null), data.getLocalDateTime());
Assert.assertEquals(data.getBooleanData(), Boolean.TRUE); Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);
Assert.assertEquals((long)data.getLongData(), 1L); Assert.assertEquals((long)data.getLongData(), 1L);
Assert.assertEquals((long)data.getIntegerData(), 1L); Assert.assertEquals((long)data.getIntegerData(), 1L);
Assert.assertEquals((long)data.getShortData(), 1L); Assert.assertEquals((long)data.getShortData(), 1L);

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

@ -3,6 +3,7 @@ package com.alibaba.easyexcel.test.core.converter;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -112,8 +113,10 @@ public class ConverterDataTest {
List<ConverterWriteData> list = new ArrayList<ConverterWriteData>(); List<ConverterWriteData> list = new ArrayList<ConverterWriteData>();
ConverterWriteData converterWriteData = new ConverterWriteData(); ConverterWriteData converterWriteData = new ConverterWriteData();
converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01"));
converterWriteData.setLocalDateTime(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null));
converterWriteData.setBooleanData(Boolean.TRUE); converterWriteData.setBooleanData(Boolean.TRUE);
converterWriteData.setBigDecimal(BigDecimal.ONE); converterWriteData.setBigDecimal(BigDecimal.ONE);
converterWriteData.setBigInteger(BigInteger.ONE);
converterWriteData.setLongData(1L); converterWriteData.setLongData(1L);
converterWriteData.setIntegerData(1); converterWriteData.setIntegerData(1);
converterWriteData.setShortData((short)1); converterWriteData.setShortData((short)1);

6
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java

@ -1,6 +1,8 @@
package com.alibaba.easyexcel.test.core.converter; package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
@ -15,10 +17,14 @@ import lombok.Data;
public class ConverterReadData { public class ConverterReadData {
@ExcelProperty("日期") @ExcelProperty("日期")
private Date date; private Date date;
@ExcelProperty("本地日期")
private LocalDateTime localDateTime;
@ExcelProperty("布尔") @ExcelProperty("布尔")
private Boolean booleanData; private Boolean booleanData;
@ExcelProperty("大数") @ExcelProperty("大数")
private BigDecimal bigDecimal; private BigDecimal bigDecimal;
@ExcelProperty("大整数")
private BigInteger bigInteger;
@ExcelProperty("长整型") @ExcelProperty("长整型")
private long longData; private long longData;
@ExcelProperty("整型") @ExcelProperty("整型")

6
src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java

@ -1,6 +1,8 @@
package com.alibaba.easyexcel.test.core.converter; package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.annotation.ExcelProperty;
@ -15,10 +17,14 @@ import lombok.Data;
public class ConverterWriteData { public class ConverterWriteData {
@ExcelProperty("日期") @ExcelProperty("日期")
private Date date; private Date date;
@ExcelProperty("本地日期")
private LocalDateTime localDateTime;
@ExcelProperty("布尔") @ExcelProperty("布尔")
private Boolean booleanData; private Boolean booleanData;
@ExcelProperty("大数") @ExcelProperty("大数")
private BigDecimal bigDecimal; private BigDecimal bigDecimal;
@ExcelProperty("大整数")
private BigInteger bigInteger;
@ExcelProperty("长整型") @ExcelProperty("长整型")
private long longData; private long longData;
@ExcelProperty("整型") @ExcelProperty("整型")

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

@ -1,6 +1,8 @@
package com.alibaba.easyexcel.test.core.converter; package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import lombok.Data; import lombok.Data;
@ -13,6 +15,9 @@ public class ReadAllConverterData {
private BigDecimal bigDecimalBoolean; private BigDecimal bigDecimalBoolean;
private BigDecimal bigDecimalNumber; private BigDecimal bigDecimalNumber;
private BigDecimal bigDecimalString; private BigDecimal bigDecimalString;
private BigInteger bigIntegerBoolean;
private BigInteger bigIntegerNumber;
private BigInteger bigIntegerString;
private Boolean booleanBoolean; private Boolean booleanBoolean;
private Boolean booleanNumber; private Boolean booleanNumber;
private Boolean booleanString; private Boolean booleanString;
@ -21,6 +26,8 @@ public class ReadAllConverterData {
private Byte byteString; private Byte byteString;
private Date dateNumber; private Date dateNumber;
private Date dateString; private Date dateString;
private LocalDateTime localDateTimeNumber;
private LocalDateTime localDateTimeString;
private Double doubleBoolean; private Double doubleBoolean;
private Double doubleNumber; private Double doubleNumber;
private Double doubleString; private Double doubleString;

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

@ -1,6 +1,7 @@
package com.alibaba.easyexcel.test.core.converter; package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -35,6 +36,9 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
Assert.assertEquals(data.getBigDecimalBoolean().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals(data.getBigDecimalBoolean().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigDecimalNumber().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals(data.getBigDecimalNumber().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigDecimalString().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals(data.getBigDecimalString().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigIntegerBoolean().intValue(), BigInteger.ONE.intValue(), 0.0);
Assert.assertEquals(data.getBigIntegerNumber().intValue(), BigInteger.ONE.intValue(), 0.0);
Assert.assertEquals(data.getBigIntegerString().intValue(), BigInteger.ONE.intValue(), 0.0);
Assert.assertTrue(data.getBooleanBoolean()); Assert.assertTrue(data.getBooleanBoolean());
Assert.assertTrue(data.getBooleanNumber()); Assert.assertTrue(data.getBooleanNumber());
Assert.assertTrue(data.getBooleanString()); Assert.assertTrue(data.getBooleanString());
@ -47,6 +51,8 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
} catch (ParseException e) { } catch (ParseException e) {
throw new ExcelCommonException("Test Exception", e); throw new ExcelCommonException("Test Exception", e);
} }
Assert.assertEquals(data.getLocalDateTimeNumber(), DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null));
Assert.assertEquals(data.getLocalDateTimeString(), DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null));
Assert.assertEquals(data.getDoubleBoolean(), 1.0, 0.0); Assert.assertEquals(data.getDoubleBoolean(), 1.0, 0.0);
Assert.assertEquals(data.getDoubleNumber(), 1.0, 0.0); Assert.assertEquals(data.getDoubleNumber(), 1.0, 0.0);
Assert.assertEquals(data.getDoubleString(), 1.0, 0.0); Assert.assertEquals(data.getDoubleString(), 1.0, 0.0);

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

Binary file not shown.

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

Binary file not shown.

4
src/test/resources/converter/converterCsv.csv

@ -1,2 +1,2 @@
大数的布尔(不支持),大数的数字,大数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期 大数的布尔(不支持),大数的数字,大数的字符串,大整数的布尔(不支持),大整数的数字,大整数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,本地日期的数字,本地日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期
1,1,1,TRUE,TRUE,TRUE,1,1,1,2020-01-01 01:01:01,2020-01-01 01:01:01,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,TRUE,1,测试,#VALUE!,2,1测试,2020-01-01 01:01:01 1,1,1,1,1,1,TRUE,TRUE,TRUE,1,1,1,2020-01-01 01:01:01,2020-01-01 01:01:01,2020-01-01 01:01:01,2020-01-01 01:01:01,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,TRUE,1,测试,#VALUE!,2,1测试,2020-01-01 01:01:01
1 大数的布尔(不支持) 大数的布尔(不支持),大数的数字,大数的字符串,大整数的布尔(不支持),大整数的数字,大整数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,本地日期的数字,本地日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期 大数的数字 大数的字符串 布尔的布尔 布尔的数字(不支持) 布尔的字符串 字节的布尔(不支持) 字节的数字 字节的字符串 日期的数字 日期的字符串 双精度浮点的布尔(不支持) 双精度浮点的数字 双精度浮点的字符串 浮点的布尔(不支持) 浮点的数字 浮点的字符串 整型的布尔(不支持) 整型的数字 整型的字符串 长整型的布尔(不支持) 长整型的数字 长整型的字符串 短整型的布尔(不支持) 短整型的数字 短整型的字符串 字符串的布尔 字符串的数字 字符串的字符串 字符串的错误 字符串的数字公式 字符串的字符串公式 字符串的数字日期
2 1 1,1,1,1,1,1,TRUE,TRUE,TRUE,1,1,1,2020-01-01 01:01:01,2020-01-01 01:01:01,2020-01-01 01:01:01,2020-01-01 01:01:01,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,TRUE,1,测试,#VALUE!,2,1测试,2020-01-01 01:01:01 1 1 TRUE TRUE TRUE 1 1 1 2020-01-01 01:01:01 2020-01-01 01:01:01 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 TRUE 1 测试 #VALUE! 2 1测试 2020-01-01 01:01:01
Loading…
Cancel
Save