解决 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;
相关推荐
Brilliantwxx2 分钟前
【Linux】 软件包管理器(yum)+ Vim使用
linux·运维·服务器·开发语言·编辑器·vim
雪的季节6 分钟前
Python 线程同步与异步编程 全解析
java·开发语言
吃饱了得干活18 分钟前
缓存与数据库一致性:从理论到实战
java·后端·面试
520拼好饭被践踏25 分钟前
JAVA+Agent学习day22
java·开发语言·后端·学习
笨蛋不要掉眼泪36 分钟前
Java虚拟机:堆的参数配置
java·开发语言·jvm
霸道流氓气质39 分钟前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
冰暮流星42 分钟前
javascript之date对象
开发语言·javascript·ecmascript
花生了什么事o44 分钟前
DDD 分层架构:六层分层架构
java·架构·ddd
Y3815326621 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存
Wang's Blog1 小时前
Go-Zero项目开发17: IM私聊功能实现与消息存储设计
开发语言·后端·golang