解决 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;
相关推荐
却话巴山夜雨时i1 分钟前
互联网大厂Java面试:从Spring到微服务的全栈挑战
java·spring boot·redis·微服务·面试·kafka·技术栈
ch.ju6 分钟前
Java程序设计(第3版)第二章——java的数据类型:字符 char
java
尘世壹俗人6 分钟前
idea提交git版本由于中文文件名卡死不动
java·git·intellij-idea
Rabitebla8 分钟前
C++ 入门基础:从 C 到 C++ 的第一步
c语言·开发语言·c++
西魏陶渊明10 分钟前
解决异步挑战:Reactor Context 实现响应式上下文传递
开发语言·python
深挖派10 分钟前
IntelliJ IDEA 2026.1 安装配置与高效开发环境搭建 (保姆级图文教程)
java·ide·intellij-idea
小则又沐风a15 分钟前
C++内存管理 C++模板
开发语言·c++
不会写DN15 分钟前
如何给 Go 语言的 TCP 聊天服务加上 ACK 可靠送达机制
开发语言·tcp/ip·golang
起个名特麻烦16 分钟前
SpringBoot全局配置LocalDate/LocalTime/LocalDateTime的序列化和反序列化
java·spring boot·后端
小李云雾19 分钟前
FastAPI 后端开发:文件上传 + 表单提交
开发语言·python·lua·postman·fastapi