StringNumberConverter.java
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.taover.easyexcel.converters.string;
import java.math.BigDecimal;
import org.apache.poi.ss.usermodel.DateUtil;
import com.taover.easyexcel.converters.Converter;
import com.taover.easyexcel.enums.CellDataTypeEnum;
import com.taover.easyexcel.metadata.CellData;
import com.taover.easyexcel.metadata.GlobalConfiguration;
import com.taover.easyexcel.metadata.property.ExcelContentProperty;
import com.taover.easyexcel.util.DateUtils;
import com.taover.easyexcel.util.NumberDataFormatterUtils;
import com.taover.easyexcel.util.NumberUtils;
import com.taover.easyexcel.util.StringUtils;
/**
* String and number converter
*
* @author Jiaju Zhuang
*/
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,
GlobalConfiguration globalConfiguration) {
// If there are "DateTimeFormat", read as date
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
return DateUtils.format(
DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
contentProperty.getDateTimeFormatProperty().getFormat());
}
// If there are "NumberFormat", read as number
if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
return NumberUtils.format(cellData.getNumberValue(), contentProperty);
}
// Excel defines formatting
if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {
return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),
cellData.getDataFormatString(), globalConfiguration);
}
// Default conversion number
return NumberUtils.format(cellData.getNumberValue(), contentProperty);
}
@Override
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
return new CellData(new BigDecimal(value));
}
}