java 实体类的数据是String类型的,想要用easyexcel导出时,导出的单元格格式是数值类型的,并且设置千分位,写了一个转换器,但是只能转换成千分位,单元格还是文本类型的
private static final DecimalFormat FORMAT = new DecimalFormat("#,##0");
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
try {
// 将字符串转换为数字进行千分位格式化
Number number = FORMAT.parse(value);
String formattedNumber = FORMAT.format(number);
// 创建WriteCellData对象并设置千分位格式化后的字符串
return new WriteCellData<>(formattedNumber);
} catch (Exception e) {
return new WriteCellData<>(value);
}
}
//又试了第二种,转换成数字了,但是千分位又没了
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
try {
// 将字符串转换为数字进行千分位格式化
Number number = FORMAT.parse(value);
// 使用BigDecimal确保精度
BigDecimal bigDecimalValue = new BigDecimal(number.toString());
// 创建WriteCellData对象并设置为数字类型
return new WriteCellData<>(bigDecimalValue);
} catch (Exception e) {
return new WriteCellData<>(value);
}
}