forked from fanruan/easyexcel
zhuangjiaju
5 years ago
17 changed files with 391 additions and 28 deletions
@ -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); |
||||
} |
||||
|
||||
} |
@ -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()); |
||||
} |
||||
|
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
|
||||
} |
@ -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()); |
||||
} |
||||
|
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
|
||||
} |
@ -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()); |
||||
} |
||||
|
||||
} |
@ -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); |
||||
} |
||||
} |
@ -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…
Reference in new issue