解决 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;
相关推荐
后端小张几秒前
【JAVA 进阶】Spring Boot自动配置详解
java·开发语言·人工智能·spring boot·后端·spring·spring cloud
郝学胜-神的一滴几秒前
Python面向对象编程:解耦、多态与魔法艺术
java·开发语言·c++·python·设计模式·软件工程
有趣灵魂2 分钟前
Java SpringBoot批量获取Minio中多个文件进行压缩成zip下载
java·开发语言·spring boot
csbysj20204 分钟前
CSS3 圆角
开发语言
IT 行者4 分钟前
Spring Security Session 序列化策略分析
java·spring boot·后端·spring
__万波__4 分钟前
二十三种设计模式(十六)--迭代器模式
java·设计模式·迭代器模式
消失的旧时光-19436 分钟前
从 Kotlin 到 Flutter:架构迁移指南
开发语言·flutter·kotlin
phil zhang10 分钟前
Celer:为大型C/C++项目打造的极简包管理器
开发语言·c++·elasticsearch
L Jiawen1 小时前
【Go · Gin】基础知识
开发语言·golang·gin
掘根2 小时前
【消息队列项目】客户端四大模块实现
开发语言·后端·ruby