解决 EasyExcel BigDecimal 加%的问题

1.看很多资料还是有问题 于是想到可以用转换器来实现

这是原来的方式

xml 复制代码
    @NumberFormat(value = "#.##%")
    private BigDecimal proportion;

请看大屏幕

xml 复制代码
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;

import java.math.BigDecimal;
import java.text.ParseException;
public class BigDecimalAddPercentConverter implements Converter<BigDecimal> {
    public BigDecimalAddPercentConverter() {
    }

    public Class<BigDecimal> supportJavaTypeKey() {
        return BigDecimal.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public BigDecimal convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
        return NumberUtils.parseBigDecimal(cellData.getStringValue(), contentProperty);
    }

    public WriteCellData<?> convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(value+"%");
    }
}

其实也很简单哈

修改后的

xml 复制代码
	@ExcelProperty(converter = BigDecimalAddPercentConverter.class)
    private BigDecimal proportion;
相关推荐
踩着两条虫8 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB8 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
budingxiaomoli8 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫8 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁9 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
即使再小的船也能远航9 小时前
【Python】安装
开发语言·python
学习中.........9 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java
Irissgwe9 小时前
类与对象(三)
开发语言·c++·类和对象·友元
计算机安禾9 小时前
【c++面向对象编程】第24篇:类型转换运算符:自定义隐式转换与explicit
java·c++·算法
雪度娃娃9 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++