提交 9b3c8f12 编写于 作者: Z zhuangjiaju

加入默认转换器

上级 058df4d7
......@@ -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
......
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);
}
}
......@@ -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());
}
}
......@@ -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);
}
}
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
}
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);
}
}
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());
}
}
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);
}
}
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);
}
}
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());
}
}
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);
}
}
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);
}
}
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());
}
}
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);
}
}
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));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册