提交 3bf01589 编写于 作者: 庄家钜's avatar 庄家钜

新增localdatetime

上级 ad5fd496
......@@ -5,6 +5,8 @@ import java.util.Map;
import com.alibaba.excel.converters.bigdecimal.BigDecimalBooleanConverter;
import com.alibaba.excel.converters.bigdecimal.BigDecimalNumberConverter;
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.booleanconverter.BooleanBooleanConverter;
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.IntegerNumberConverter;
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.LongNumberConverter;
import com.alibaba.excel.converters.longconverter.LongStringConverter;
......@@ -61,6 +66,10 @@ public class DefaultConverterLoader {
putAllConverter(new BigDecimalNumberConverter());
putAllConverter(new BigDecimalStringConverter());
putAllConverter(new BigIntegerBooleanConverter());
putAllConverter(new BigIntegerNumberConverter());
putAllConverter(new BigIntegerStringConverter());
putAllConverter(new BooleanBooleanConverter());
putAllConverter(new BooleanNumberConverter());
putAllConverter(new BooleanStringConverter());
......@@ -72,6 +81,9 @@ public class DefaultConverterLoader {
putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter());
putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateTimeStringConverter());
putAllConverter(new DoubleBooleanConverter());
putAllConverter(new DoubleNumberConverter());
putAllConverter(new DoubleStringConverter());
......@@ -103,9 +115,11 @@ public class DefaultConverterLoader {
private static void initDefaultWriteConverter() {
defaultWriteConverter = MapUtils.newHashMapWithExpectedSize(40);
putWriteConverter(new BigDecimalNumberConverter());
putWriteConverter(new BigIntegerNumberConverter());
putWriteConverter(new BooleanBooleanConverter());
putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter());
......@@ -120,9 +134,11 @@ public class DefaultConverterLoader {
// In some cases, it must be converted to string
putWriteStringConverter(new BigDecimalStringConverter());
putWriteStringConverter(new BigIntegerStringConverter());
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter());
putWriteStringConverter(new IntegerStringConverter());
......
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);
}
}
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);
}
}
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())));
}
}
}
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;
}
}
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()));
}
}
}
package com.alibaba.excel.metadata.data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
......@@ -22,7 +24,8 @@ public class WriteCellData<T> extends CellData<T> {
/**
* Support only when writing.{@link CellDataTypeEnum#DATE}
*/
private Date dateValue;
private LocalDateTime dateValue;
/**
* rich text.{@link CellDataTypeEnum#RICH_TEXT_STRING}
*/
......@@ -84,6 +87,15 @@ public class WriteCellData<T> extends CellData<T> {
}
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();
if (dateValue == null) {
throw new IllegalArgumentException("DateValue can not be null");
......
......@@ -3,8 +3,11 @@ package com.alibaba.excel.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
......@@ -69,6 +72,25 @@ public class DateUtils {
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
*
......@@ -86,7 +108,7 @@ public class DateUtils {
* @param dateString
* @return
*/
private static String switchDateFormat(String dateString) {
public static String switchDateFormat(String dateString) {
int length = dateString.length();
switch (length) {
case 19:
......@@ -141,6 +163,27 @@ public class DateUtils {
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) {
Map<String, SimpleDateFormat> dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get();
if (dateFormatMap == null) {
......
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
......@@ -36,8 +37,10 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterReadDa
} catch (ParseException 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.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.getIntegerData(), 1L);
Assert.assertEquals((long)data.getShortData(), 1L);
......
......@@ -3,6 +3,7 @@ package com.alibaba.easyexcel.test.core.converter;
import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
......@@ -112,8 +113,10 @@ public class ConverterDataTest {
List<ConverterWriteData> list = new ArrayList<ConverterWriteData>();
ConverterWriteData converterWriteData = new ConverterWriteData();
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.setBigDecimal(BigDecimal.ONE);
converterWriteData.setBigInteger(BigInteger.ONE);
converterWriteData.setLongData(1L);
converterWriteData.setIntegerData(1);
converterWriteData.setShortData((short)1);
......
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
......@@ -15,10 +17,14 @@ import lombok.Data;
public class ConverterReadData {
@ExcelProperty("日期")
private Date date;
@ExcelProperty("本地日期")
private LocalDateTime localDateTime;
@ExcelProperty("布尔")
private Boolean booleanData;
@ExcelProperty("大数")
private BigDecimal bigDecimal;
@ExcelProperty("大整数")
private BigInteger bigInteger;
@ExcelProperty("长整型")
private long longData;
@ExcelProperty("整型")
......
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date;
import com.alibaba.excel.annotation.ExcelProperty;
......@@ -15,10 +17,14 @@ import lombok.Data;
public class ConverterWriteData {
@ExcelProperty("日期")
private Date date;
@ExcelProperty("本地日期")
private LocalDateTime localDateTime;
@ExcelProperty("布尔")
private Boolean booleanData;
@ExcelProperty("大数")
private BigDecimal bigDecimal;
@ExcelProperty("大整数")
private BigInteger bigInteger;
@ExcelProperty("长整型")
private long longData;
@ExcelProperty("整型")
......
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Date;
import lombok.Data;
......@@ -13,6 +15,9 @@ public class ReadAllConverterData {
private BigDecimal bigDecimalBoolean;
private BigDecimal bigDecimalNumber;
private BigDecimal bigDecimalString;
private BigInteger bigIntegerBoolean;
private BigInteger bigIntegerNumber;
private BigInteger bigIntegerString;
private Boolean booleanBoolean;
private Boolean booleanNumber;
private Boolean booleanString;
......@@ -21,6 +26,8 @@ public class ReadAllConverterData {
private Byte byteString;
private Date dateNumber;
private Date dateString;
private LocalDateTime localDateTimeNumber;
private LocalDateTime localDateTimeString;
private Double doubleBoolean;
private Double doubleNumber;
private Double doubleString;
......
package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.ArrayList;
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.getBigDecimalNumber().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.getBooleanNumber());
Assert.assertTrue(data.getBooleanString());
......@@ -47,6 +51,8 @@ public class ReadAllConverterDataListener extends AnalysisEventListener<ReadAllC
} catch (ParseException 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.getDoubleNumber(), 1.0, 0.0);
Assert.assertEquals(data.getDoubleString(), 1.0, 0.0);
......
大数的布尔(不支持),大数的数字,大数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期
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
\ No newline at end of file
大数的布尔(不支持),大数的数字,大数的字符串,大整数的布尔(不支持),大整数的数字,大整数的字符串,布尔的布尔,布尔的数字(不支持),布尔的字符串,字节的布尔(不支持),字节的数字,字节的字符串,日期的数字,日期的字符串,本地日期的数字,本地日期的字符串,双精度浮点的布尔(不支持),双精度浮点的数字,双精度浮点的字符串,浮点的布尔(不支持),浮点的数字,浮点的字符串,整型的布尔(不支持),整型的数字,整型的字符串,长整型的布尔(不支持),长整型的数字,长整型的字符串,短整型的布尔(不支持),短整型的数字,短整型的字符串,字符串的布尔,字符串的数字,字符串的字符串,字符串的错误,字符串的数字公式,字符串的字符串公式,字符串的数字日期
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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册