From b1950cf8c89988dfc6a747758b3d0673283772fc Mon Sep 17 00:00:00 2001 From: zhuangjiaju Date: Tue, 16 Jul 2019 15:05:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=A8=E5=85=A8=E9=83=A8=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/converters/ConverterKey.java | 4 + .../converters/DefaultConverterLoader.java | 79 ++++++++++++++++++- .../BigDecimalBooleanConverter.java | 3 +- ...nverter.java => LongBooleanConverter.java} | 14 ++-- .../shortconverter/ShortBooleanConverter.java | 43 ++++++++++ .../shortconverter/ShortNumberConverter.java | 35 ++++++++ .../shortconverter/ShortStringConverter.java | 35 ++++++++ .../string/StringBooleanConverter.java | 35 ++++++++ .../string/StringErrorConverter.java | 34 ++++++++ .../string/StringNumberConverter.java | 46 +++++++++++ .../com/alibaba/excel/util/NumberUtils.java | 19 ++++- 11 files changed, 331 insertions(+), 16 deletions(-) rename src/main/java/com/alibaba/excel/converters/longconverter/{IntegerBooleanConverter.java => LongBooleanConverter.java} (64%) create mode 100644 src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java create mode 100644 src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java create mode 100644 src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java create mode 100644 src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java create mode 100644 src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java create mode 100644 src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java diff --git a/src/main/java/com/alibaba/excel/converters/ConverterKey.java b/src/main/java/com/alibaba/excel/converters/ConverterKey.java index 037ba81..cbbfedc 100644 --- a/src/main/java/com/alibaba/excel/converters/ConverterKey.java +++ b/src/main/java/com/alibaba/excel/converters/ConverterKey.java @@ -24,6 +24,10 @@ public class ConverterKey { return new ConverterKey(javaTypeKey, excelTypeKey); } + public static ConverterKey buildConverterKey(Converter converter) { + return buildConverterKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()); + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index 82dcfe9..722ee9b 100644 --- a/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java @@ -3,8 +3,29 @@ package com.alibaba.excel.converters; import java.util.HashMap; 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.booleanconverter.BooleanBooleanConverter; +import com.alibaba.excel.converters.booleanconverter.BooleanNumberConverter; +import com.alibaba.excel.converters.booleanconverter.BooleanStringConverter; +import com.alibaba.excel.converters.byteconverter.ByteBooleanConverter; +import com.alibaba.excel.converters.byteconverter.ByteNumberConverter; +import com.alibaba.excel.converters.byteconverter.ByteStringConverter; +import com.alibaba.excel.converters.date.DateNumberConverter; import com.alibaba.excel.converters.date.DateStringConverter; +import com.alibaba.excel.converters.doubleconverter.DoubleNumberConverter; +import com.alibaba.excel.converters.doubleconverter.DoubleStringConverter; +import com.alibaba.excel.converters.floatconverter.FloatNumberConverter; +import com.alibaba.excel.converters.floatconverter.FloatStringConverter; +import com.alibaba.excel.converters.integer.IntegerNumberConverter; +import com.alibaba.excel.converters.integer.IntegerStringConverter; +import com.alibaba.excel.converters.longconverter.LongNumberConverter; +import com.alibaba.excel.converters.longconverter.LongStringConverter; +import com.alibaba.excel.converters.shortconverter.ShortNumberConverter; +import com.alibaba.excel.converters.shortconverter.ShortStringConverter; +import com.alibaba.excel.converters.string.StringErrorConverter; +import com.alibaba.excel.converters.string.StringNumberConverter; import com.alibaba.excel.converters.string.StringStringConverter; /** @@ -20,13 +41,20 @@ public class DefaultConverterLoader { */ public static Map loadDefaultWriteConverter() { Map converterMap = new HashMap(); - putConverter(converterMap, new DateStringConverter()); - putConverter(converterMap, new BigDecimalNumberConverter()); - putConverter(converterMap, new StringStringConverter()); + putWriteConverter(converterMap, new BigDecimalNumberConverter()); + putWriteConverter(converterMap, new BooleanBooleanConverter()); + putWriteConverter(converterMap, new ByteNumberConverter()); + putWriteConverter(converterMap, new DateStringConverter()); + putWriteConverter(converterMap, new DoubleNumberConverter()); + putWriteConverter(converterMap, new FloatNumberConverter()); + putWriteConverter(converterMap, new IntegerNumberConverter()); + putWriteConverter(converterMap, new LongNumberConverter()); + putWriteConverter(converterMap, new ShortNumberConverter()); + putWriteConverter(converterMap, new StringStringConverter()); return converterMap; } - private static void putConverter(Map converterMap, Converter converter) { + private static void putWriteConverter(Map converterMap, Converter converter) { converterMap.put(converter.supportJavaTypeKey(), converter); } @@ -37,6 +65,49 @@ public class DefaultConverterLoader { */ public static Map loadDefaultReadConverter() { Map converterMap = new HashMap(); + putReadConverter(converterMap, new BigDecimalBooleanConverter()); + putReadConverter(converterMap, new BigDecimalNumberConverter()); + putReadConverter(converterMap, new BigDecimalStringConverter()); + + putReadConverter(converterMap, new BooleanBooleanConverter()); + putReadConverter(converterMap, new BooleanNumberConverter()); + putReadConverter(converterMap, new BooleanStringConverter()); + + putReadConverter(converterMap, new ByteBooleanConverter()); + putReadConverter(converterMap, new ByteNumberConverter()); + putReadConverter(converterMap, new ByteStringConverter()); + + putReadConverter(converterMap, new DateNumberConverter()); + putReadConverter(converterMap, new DateStringConverter()); + + putReadConverter(converterMap, new DoubleNumberConverter()); + putReadConverter(converterMap, new DoubleNumberConverter()); + putReadConverter(converterMap, new DoubleStringConverter()); + + putReadConverter(converterMap, new FloatNumberConverter()); + putReadConverter(converterMap, new FloatNumberConverter()); + putReadConverter(converterMap, new FloatStringConverter()); + + putReadConverter(converterMap, new IntegerNumberConverter()); + putReadConverter(converterMap, new IntegerNumberConverter()); + putReadConverter(converterMap, new IntegerStringConverter()); + + putReadConverter(converterMap, new LongNumberConverter()); + putReadConverter(converterMap, new LongNumberConverter()); + putReadConverter(converterMap, new LongStringConverter()); + + putReadConverter(converterMap, new ShortNumberConverter()); + putReadConverter(converterMap, new ShortNumberConverter()); + putReadConverter(converterMap, new ShortStringConverter()); + + putReadConverter(converterMap, new StringNumberConverter()); + putReadConverter(converterMap, new StringNumberConverter()); + putReadConverter(converterMap, new StringStringConverter()); + putReadConverter(converterMap, new StringErrorConverter()); return converterMap; } + + private static void putReadConverter(Map converterMap, Converter converter) { + converterMap.put(ConverterKey.buildConverterKey(converter), converter); + } } diff --git a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java index 9ce4770..bba16ac 100644 --- a/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java +++ b/src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java @@ -16,7 +16,7 @@ public class BigDecimalBooleanConverter implements Converter { @Override public Class supportJavaTypeKey() { - return BigDecimal.class; + return Byte.class; } @Override @@ -39,4 +39,5 @@ public class BigDecimalBooleanConverter implements Converter { } return new CellData(Boolean.FALSE); } + } diff --git a/src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java similarity index 64% rename from src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java rename to src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java index b1b9ba3..0d500e1 100644 --- a/src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java +++ b/src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java @@ -6,17 +6,17 @@ import com.alibaba.excel.metadata.CellData; import com.alibaba.excel.metadata.property.ExcelContentProperty; /** - * Integer and boolean converter + * Long and boolean converter * * @author zhuangjiaju */ -public class IntegerBooleanConverter implements Converter { - private static final Integer ONE = 1; - private static final Integer ZERO = 0; +public class LongBooleanConverter implements Converter { + private static final Long ONE = 1L; + private static final Long ZERO = 0L; @Override public Class supportJavaTypeKey() { - return Integer.class; + return Long.class; } @Override @@ -25,7 +25,7 @@ public class IntegerBooleanConverter implements Converter { } @Override - public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { if (cellData.getBooleanValue()) { return ONE; } @@ -33,7 +33,7 @@ public class IntegerBooleanConverter implements Converter { } @Override - public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) { + public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) { if (ONE.equals(value)) { return new CellData(Boolean.TRUE); } diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java new file mode 100644 index 0000000..246f2e9 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java @@ -0,0 +1,43 @@ +package com.alibaba.excel.converters.shortconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; + +/** + * Short and boolean converter + * + * @author zhuangjiaju + */ +public class ShortBooleanConverter implements Converter { + private static final Short ONE = 1; + private static final Short ZERO = 0; + + @Override + public Class supportJavaTypeKey() { + return Long.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.BOOLEAN; + } + + @Override + public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + if (cellData.getBooleanValue()) { + return ONE; + } + return ZERO; + } + + @Override + public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { + if (ONE.equals(value)) { + return new CellData(Boolean.TRUE); + } + return new CellData(Boolean.FALSE); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java new file mode 100644 index 0000000..03435c8 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.shortconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; + +/** + * Short and number converter + * + * @author zhuangjiaju + */ +public class ShortNumberConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Short.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + return cellData.getDoubleValue().shortValue(); + } + + @Override + public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { + return new CellData(value.doubleValue()); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java new file mode 100644 index 0000000..40917b2 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.shortconverter; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.util.NumberUtils; + +/** + * Short and string converter + * + * @author zhuangjiaju + */ +public class ShortStringConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return Short.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.STRING; + } + + @Override + public Short convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + return Short.valueOf(cellData.getStringValue()); + } + + @Override + public CellData convertToExcelData(Short value, ExcelContentProperty contentProperty) { + return NumberUtils.formatToCellData(value, contentProperty); + } +} diff --git a/src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java b/src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java new file mode 100644 index 0000000..138df61 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java @@ -0,0 +1,35 @@ +package com.alibaba.excel.converters.string; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; + +/** + * String and boolean converter + * + * @author zhuangjiaju + */ +public class StringBooleanConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return String.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.BOOLEAN; + } + + @Override + public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + return cellData.getBooleanValue().toString(); + } + + @Override + public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { + return new CellData(Boolean.valueOf(value)); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java b/src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java new file mode 100644 index 0000000..47439a0 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java @@ -0,0 +1,34 @@ +package com.alibaba.excel.converters.string; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; + +/** + * String and error converter + * + * @author zhuangjiaju + */ +public class StringErrorConverter implements Converter { + @Override + public Class supportJavaTypeKey() { + return String.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.ERROR; + } + + @Override + public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + return cellData.getStringValue(); + } + + @Override + public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { + return new CellData(CellDataTypeEnum.ERROR, value); + } + +} diff --git a/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java new file mode 100644 index 0000000..0f0fe69 --- /dev/null +++ b/src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java @@ -0,0 +1,46 @@ +package com.alibaba.excel.converters.string; + +import org.apache.poi.hssf.usermodel.HSSFDateUtil; + +import com.alibaba.excel.converters.Converter; +import com.alibaba.excel.enums.CellDataTypeEnum; +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.property.ExcelContentProperty; +import com.alibaba.excel.util.DateUtils; +import com.alibaba.excel.util.NumberUtils; + +/** + * String and number converter + * + * @author zhuangjiaju + */ +public class StringNumberConverter implements Converter { + + @Override + public Class supportJavaTypeKey() { + return String.class; + } + + @Override + public CellDataTypeEnum supportExcelTypeKey() { + return CellDataTypeEnum.NUMBER; + } + + @Override + public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { + // If there are "DateTimeFormat", read as date + if (contentProperty.getDateTimeFormatProperty() != null) { + return DateUtils.format( + HSSFDateUtil.getJavaDate(cellData.getDoubleValue(), + contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null), + contentProperty.getDateTimeFormatProperty().getFormat()); + } + // If there are "NumberFormat", read as number + return NumberUtils.format(cellData.getDoubleValue(), contentProperty); + } + + @Override + public CellData convertToExcelData(String value, ExcelContentProperty contentProperty) { + return new CellData(Double.valueOf(value)); + } +} diff --git a/src/main/java/com/alibaba/excel/util/NumberUtils.java b/src/main/java/com/alibaba/excel/util/NumberUtils.java index b65efdf..3b5c48c 100644 --- a/src/main/java/com/alibaba/excel/util/NumberUtils.java +++ b/src/main/java/com/alibaba/excel/util/NumberUtils.java @@ -16,20 +16,31 @@ public class NumberUtils { /** * format - * + * * @param num * @param contentProperty * @return */ - public static CellData formatToCellData(Number num, ExcelContentProperty contentProperty) { + public static String format(Number num, ExcelContentProperty contentProperty) { if (contentProperty.getNumberFormatProperty() == null || StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) { - return new CellData(num.toString()); + return num.toString(); } String format = contentProperty.getNumberFormatProperty().getFormat(); RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode(); DecimalFormat decimalFormat = new DecimalFormat(format); decimalFormat.setRoundingMode(roundingMode); - return new CellData(decimalFormat.format(num)); + return decimalFormat.format(num); + } + + /** + * format + * + * @param num + * @param contentProperty + * @return + */ + public static CellData formatToCellData(Number num, ExcelContentProperty contentProperty) { + return new CellData(format(num, contentProperty)); } }