diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 02f00313..fda84b84 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/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.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()); diff --git a/src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerBooleanConverter.java new file mode 100644 index 00000000..0ac0e6c9 --- /dev/null +++ b/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 { + + @Override + public Class 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); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerNumberConverter.java b/src/main/java/com/alibaba/excel/converters/biginteger/BigIntegerNumberConverter.java new file mode 100644 index 00000000..c5957a15 --- /dev/null +++ b/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 { + + @Override + public Class 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); + } +} diff --git a/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java b/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java new file mode 100644 index 00000000..f57f0bdc --- /dev/null +++ b/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 { + + @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()))); + } + } +} diff --git a/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java b/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java new file mode 100644 index 00000000..072a7864 --- /dev/null +++ b/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 { + @Override + public Class 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; + } +} diff --git a/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeStringConverter.java b/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeStringConverter.java new file mode 100644 index 00000000..56aee318 --- /dev/null +++ b/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 { + @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())); + } + } +} diff --git a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index 4c54b6fa..ed522b81 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java @@ -1,6 +1,8 @@ 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 extends CellData { /** * 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 extends CellData { } 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"); diff --git a/src/main/java/com/alibaba/excel/util/DateUtils.java b/src/main/java/com/alibaba/excel/util/DateUtils.java index 68eb01c0..4a297cf7 100644 --- a/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/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.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 dateFormatMap = DATE_FORMAT_THREAD_LOCAL.get(); if (dateFormatMap == null) { diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index 411700f0..0a71d25a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java @@ -1,6 +1,7 @@ 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 list = new ArrayList(); 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); diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java index bf7af176..8fa78f7e 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java @@ -1,6 +1,8 @@ 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("整型") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java index 009fae2e..68c34423 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java @@ -1,6 +1,8 @@ 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("整型") diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java index 4f758a02..cccee710 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterData.java @@ -1,6 +1,8 @@ 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; diff --git a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index 4a11f8b8..8516cd5b 100644 --- a/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java @@ -1,6 +1,7 @@ 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