Browse Source

补全全部转换器

developing
zhuangjiaju 5 years ago
parent
commit
b1950cf8c8
  1. 4
      src/main/java/com/alibaba/excel/converters/ConverterKey.java
  2. 79
      src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  3. 3
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalBooleanConverter.java
  4. 14
      src/main/java/com/alibaba/excel/converters/longconverter/LongBooleanConverter.java
  5. 43
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortBooleanConverter.java
  6. 35
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortNumberConverter.java
  7. 35
      src/main/java/com/alibaba/excel/converters/shortconverter/ShortStringConverter.java
  8. 35
      src/main/java/com/alibaba/excel/converters/string/StringBooleanConverter.java
  9. 34
      src/main/java/com/alibaba/excel/converters/string/StringErrorConverter.java
  10. 46
      src/main/java/com/alibaba/excel/converters/string/StringNumberConverter.java
  11. 17
      src/main/java/com/alibaba/excel/util/NumberUtils.java

4
src/main/java/com/alibaba/excel/converters/ConverterKey.java

@ -24,6 +24,10 @@ public class ConverterKey {
return new ConverterKey(javaTypeKey, excelTypeKey); return new ConverterKey(javaTypeKey, excelTypeKey);
} }
public static ConverterKey buildConverterKey(Converter converter) {
return buildConverterKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey());
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) { if (this == o) {

79
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.HashMap;
import java.util.Map; import java.util.Map;
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.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.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; import com.alibaba.excel.converters.string.StringStringConverter;
/** /**
@ -20,13 +41,20 @@ public class DefaultConverterLoader {
*/ */
public static Map<Class, Converter> loadDefaultWriteConverter() { public static Map<Class, Converter> loadDefaultWriteConverter() {
Map<Class, Converter> converterMap = new HashMap<Class, Converter>(); Map<Class, Converter> converterMap = new HashMap<Class, Converter>();
putConverter(converterMap, new DateStringConverter()); putWriteConverter(converterMap, new BigDecimalNumberConverter());
putConverter(converterMap, new BigDecimalNumberConverter()); putWriteConverter(converterMap, new BooleanBooleanConverter());
putConverter(converterMap, new StringStringConverter()); 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; return converterMap;
} }
private static void putConverter(Map<Class, Converter> converterMap, Converter converter) { private static void putWriteConverter(Map<Class, Converter> converterMap, Converter converter) {
converterMap.put(converter.supportJavaTypeKey(), converter); converterMap.put(converter.supportJavaTypeKey(), converter);
} }
@ -37,6 +65,49 @@ public class DefaultConverterLoader {
*/ */
public static Map<ConverterKey, Converter> loadDefaultReadConverter() { public static Map<ConverterKey, Converter> loadDefaultReadConverter() {
Map<ConverterKey, Converter> converterMap = new HashMap<ConverterKey, Converter>(); Map<ConverterKey, Converter> converterMap = new HashMap<ConverterKey, Converter>();
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; return converterMap;
} }
private static void putReadConverter(Map<ConverterKey, Converter> converterMap, Converter converter) {
converterMap.put(ConverterKey.buildConverterKey(converter), converter);
}
} }

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

@ -16,7 +16,7 @@ public class BigDecimalBooleanConverter implements Converter<BigDecimal> {
@Override @Override
public Class supportJavaTypeKey() { public Class supportJavaTypeKey() {
return BigDecimal.class; return Byte.class;
} }
@Override @Override
@ -39,4 +39,5 @@ public class BigDecimalBooleanConverter implements Converter<BigDecimal> {
} }
return new CellData(Boolean.FALSE); return new CellData(Boolean.FALSE);
} }
} }

14
src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java → 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; import com.alibaba.excel.metadata.property.ExcelContentProperty;
/** /**
* Integer and boolean converter * Long and boolean converter
* *
* @author zhuangjiaju * @author zhuangjiaju
*/ */
public class IntegerBooleanConverter implements Converter<Integer> { public class LongBooleanConverter implements Converter<Long> {
private static final Integer ONE = 1; private static final Long ONE = 1L;
private static final Integer ZERO = 0; private static final Long ZERO = 0L;
@Override @Override
public Class supportJavaTypeKey() { public Class supportJavaTypeKey() {
return Integer.class; return Long.class;
} }
@Override @Override
@ -25,7 +25,7 @@ public class IntegerBooleanConverter implements Converter<Integer> {
} }
@Override @Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) { public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
if (cellData.getBooleanValue()) { if (cellData.getBooleanValue()) {
return ONE; return ONE;
} }
@ -33,7 +33,7 @@ public class IntegerBooleanConverter implements Converter<Integer> {
} }
@Override @Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) { public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) {
if (ONE.equals(value)) { if (ONE.equals(value)) {
return new CellData(Boolean.TRUE); return new CellData(Boolean.TRUE);
} }

43
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<Short> {
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);
}
}

35
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<Short> {
@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());
}
}

35
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<Short> {
@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);
}
}

35
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<String> {
@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));
}
}

34
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<String> {
@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);
}
}

46
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<String> {
@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));
}
}

17
src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -21,15 +21,26 @@ public class NumberUtils {
* @param contentProperty * @param contentProperty
* @return * @return
*/ */
public static CellData formatToCellData(Number num, ExcelContentProperty contentProperty) { public static String format(Number num, ExcelContentProperty contentProperty) {
if (contentProperty.getNumberFormatProperty() == null if (contentProperty.getNumberFormatProperty() == null
|| StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) { || StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) {
return new CellData(num.toString()); return num.toString();
} }
String format = contentProperty.getNumberFormatProperty().getFormat(); String format = contentProperty.getNumberFormatProperty().getFormat();
RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode(); RoundingMode roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
DecimalFormat decimalFormat = new DecimalFormat(format); DecimalFormat decimalFormat = new DecimalFormat(format);
decimalFormat.setRoundingMode(roundingMode); 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));
} }
} }

Loading…
Cancel
Save