Browse Source

加入默认转换器

bugfix
zhuangjiaju 5 years ago
parent
commit
9b3c8f129c
  1. 2
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java
  2. 17
      src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java
  3. 2
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java
  4. 3
      src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java
  5. 14
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleBooleanConverter.java
  6. 4
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java
  7. 3
      src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java
  8. 43
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatBooleanConverter.java
  9. 35
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatNumberConverter.java
  10. 35
      src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java
  11. 43
      src/main/java/com/alibaba/excel/converters/integer/IntegerBooleanConverter.java
  12. 35
      src/main/java/com/alibaba/excel/converters/integer/IntegerNumberConverter.java
  13. 35
      src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java
  14. 43
      src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java
  15. 35
      src/main/java/com/alibaba/excel/converters/longconverter/LongNumberConverter.java
  16. 35
      src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java
  17. 35
      src/main/java/com/alibaba/excel/util/NumberUtils.java

2
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalNumberConverter.java

@ -26,7 +26,7 @@ public class BigDecimalNumberConverter implements Converter<BigDecimal> {
@Override
public BigDecimal convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return new BigDecimal(cellData.getDoubleValue());
return BigDecimal.valueOf(cellData.getDoubleValue());
}
@Override

17
src/main/java/com/alibaba/excel/converters/bigdecimal/BigDecimalStringConverter.java

@ -1,14 +1,12 @@
package com.alibaba.excel.converters.bigdecimal;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
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.StringUtils;
import com.alibaba.excel.util.NumberUtils;
/**
* BigDecimal and string converter
@ -34,17 +32,6 @@ public class BigDecimalStringConverter implements Converter<BigDecimal> {
@Override
public CellData convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty) {
String format = null;
RoundingMode roundingMode = RoundingMode.HALF_UP;
if (contentProperty.getNumberFormatProperty() != null) {
format = contentProperty.getNumberFormatProperty().getFormat();
roundingMode = contentProperty.getNumberFormatProperty().getRoundingMode();
}
if (StringUtils.isEmpty(format)) {
return new CellData(value.toString());
}
DecimalFormat decimalFormat = new DecimalFormat(format);
decimalFormat.setRoundingMode(roundingMode);
return new CellData(decimalFormat.format(value));
return NumberUtils.formatToCellData(value, contentProperty);
}
}

2
src/main/java/com/alibaba/excel/converters/byteconverter/ByteNumberConverter.java

@ -29,7 +29,7 @@ public class ByteNumberConverter implements Converter<Byte> {
@Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) {
return new CellData((double)value);
return new CellData(value.doubleValue());
}
}

3
src/main/java/com/alibaba/excel/converters/byteconverter/ByteStringConverter.java

@ -4,6 +4,7 @@ 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;
/**
* Byte and string converter
@ -29,7 +30,7 @@ public class ByteStringConverter implements Converter<Byte> {
@Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) {
return new CellData(value.toString());
return NumberUtils.formatToCellData(value, contentProperty);
}
}

14
src/main/java/com/alibaba/excel/converters/doubleconverter/ByteBooleanConverter.java → src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleBooleanConverter.java

@ -6,17 +6,17 @@ import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
/**
* Byte and boolean converter
* Double and boolean converter
*
* @author zhuangjiaju
*/
public class ByteBooleanConverter implements Converter<Byte> {
private static final Byte ONE = (byte)1;
private static final Byte ZERO = (byte)0;
public class DoubleBooleanConverter implements Converter<Double> {
private static final Double ONE = 1.0;
private static final Double ZERO = 0.0;
@Override
public Class supportJavaTypeKey() {
return Byte.class;
return Double.class;
}
@Override
@ -25,7 +25,7 @@ public class ByteBooleanConverter implements Converter<Byte> {
}
@Override
public Byte convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
if (cellData.getBooleanValue()) {
return ONE;
}
@ -33,7 +33,7 @@ public class ByteBooleanConverter implements Converter<Byte> {
}
@Override
public CellData convertToExcelData(Byte value, ExcelContentProperty contentProperty) {
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) {
if (ONE.equals(value)) {
return new CellData(Boolean.TRUE);
}

4
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleNumberConverter.java

@ -23,12 +23,12 @@ public class DoubleNumberConverter implements Converter<Double> {
}
@Override
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) throws Exception {
public Double convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return cellData.getDoubleValue();
}
@Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) throws Exception {
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) {
return new CellData(value);
}

3
src/main/java/com/alibaba/excel/converters/doubleconverter/DoubleStringConverter.java

@ -4,6 +4,7 @@ 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;
/**
* Double and string converter
@ -29,6 +30,6 @@ public class DoubleStringConverter implements Converter<Double> {
@Override
public CellData convertToExcelData(Double value, ExcelContentProperty contentProperty) {
return new CellData(value.toString());
return NumberUtils.formatToCellData(value, contentProperty);
}
}

43
src/main/java/com/alibaba/excel/converters/floatconverter/FloatBooleanConverter.java

@ -0,0 +1,43 @@
package com.alibaba.excel.converters.floatconverter;
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;
/**
* Float and boolean converter
*
* @author zhuangjiaju
*/
public class FloatBooleanConverter implements Converter<Float> {
private static final Float ONE = (float)1.0;
private static final Float ZERO = (float)0.0;
@Override
public Class supportJavaTypeKey() {
return Float.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.BOOLEAN;
}
@Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
if (cellData.getBooleanValue()) {
return ONE;
}
return ZERO;
}
@Override
public CellData convertToExcelData(Float 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/floatconverter/FloatNumberConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.floatconverter;
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;
/**
* Float and number converter
*
* @author zhuangjiaju
*/
public class FloatNumberConverter implements Converter<Float> {
@Override
public Class supportJavaTypeKey() {
return Float.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return cellData.getDoubleValue().floatValue();
}
@Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty) {
return new CellData(value.doubleValue());
}
}

35
src/main/java/com/alibaba/excel/converters/floatconverter/FloatStringConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.floatconverter;
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;
/**
* Float and string converter
*
* @author zhuangjiaju
*/
public class FloatStringConverter implements Converter<Float> {
@Override
public Class supportJavaTypeKey() {
return Float.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Float convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return Float.valueOf(cellData.getStringValue());
}
@Override
public CellData convertToExcelData(Float value, ExcelContentProperty contentProperty) {
return NumberUtils.formatToCellData(value, contentProperty);
}
}

43
src/main/java/com/alibaba/excel/converters/integer/IntegerBooleanConverter.java

@ -0,0 +1,43 @@
package com.alibaba.excel.converters.integer;
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;
/**
* Integer and boolean converter
*
* @author zhuangjiaju
*/
public class IntegerBooleanConverter implements Converter<Integer> {
private static final Integer ONE = 1;
private static final Integer ZERO = 0;
@Override
public Class supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.BOOLEAN;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
if (cellData.getBooleanValue()) {
return ONE;
}
return ZERO;
}
@Override
public CellData convertToExcelData(Integer 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/integer/IntegerNumberConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.integer;
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;
/**
* Integer and number converter
*
* @author zhuangjiaju
*/
public class IntegerNumberConverter implements Converter<Integer> {
@Override
public Class supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return cellData.getDoubleValue().intValue();
}
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) {
return new CellData(value.doubleValue());
}
}

35
src/main/java/com/alibaba/excel/converters/integer/IntegerStringConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.integer;
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;
/**
* Integer and string converter
*
* @author zhuangjiaju
*/
public class IntegerStringConverter implements Converter<Integer> {
@Override
public Class supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return Integer.valueOf(cellData.getStringValue());
}
@Override
public CellData convertToExcelData(Integer value, ExcelContentProperty contentProperty) {
return NumberUtils.formatToCellData(value, contentProperty);
}
}

43
src/main/java/com/alibaba/excel/converters/longconverter/IntegerBooleanConverter.java

@ -0,0 +1,43 @@
package com.alibaba.excel.converters.longconverter;
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;
/**
* Integer and boolean converter
*
* @author zhuangjiaju
*/
public class IntegerBooleanConverter implements Converter<Integer> {
private static final Integer ONE = 1;
private static final Integer ZERO = 0;
@Override
public Class supportJavaTypeKey() {
return Integer.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.BOOLEAN;
}
@Override
public Integer convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
if (cellData.getBooleanValue()) {
return ONE;
}
return ZERO;
}
@Override
public CellData convertToExcelData(Integer 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/longconverter/LongNumberConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.longconverter;
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;
/**
* Long and number converter
*
* @author zhuangjiaju
*/
public class LongNumberConverter implements Converter<Long> {
@Override
public Class supportJavaTypeKey() {
return Long.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return cellData.getDoubleValue().longValue();
}
@Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) {
return new CellData(value.doubleValue());
}
}

35
src/main/java/com/alibaba/excel/converters/longconverter/LongStringConverter.java

@ -0,0 +1,35 @@
package com.alibaba.excel.converters.longconverter;
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;
/**
* Long and string converter
*
* @author zhuangjiaju
*/
public class LongStringConverter implements Converter<Long> {
@Override
public Class supportJavaTypeKey() {
return Long.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty) {
return Long.valueOf(cellData.getStringValue());
}
@Override
public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty) {
return NumberUtils.formatToCellData(value, contentProperty);
}
}

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

@ -0,0 +1,35 @@
package com.alibaba.excel.util;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
/**
* Number utils
*
* @author zhuangjiaju
*/
public class NumberUtils {
private NumberUtils() {}
/**
* format
*
* @param num
* @param contentProperty
* @return
*/
public static CellData formatToCellData(Number num, ExcelContentProperty contentProperty) {
if (contentProperty.getNumberFormatProperty() == null
|| StringUtils.isEmpty(contentProperty.getNumberFormatProperty().getFormat())) {
return new CellData(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));
}
}
Loading…
Cancel
Save