解决 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;
相关推荐
一 乐2 小时前
动漫书销售商城|基于springboot + vue动漫书销售商城(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
卓怡学长2 小时前
w214基于jsp知道特产网
java·intellij-idea
步行cgn2 小时前
Spring 注解使用详解
java·spring
一条小小yu2 小时前
Spring IoC的理解
java·后端·spring
落魄实习生3 小时前
Agent Scope Java 2.x 系列【7】工具使用
java·开发语言·ai
旺仔学长 哈哈3 小时前
springboot钓鱼爱好者交流平台APP设计与实现
java·spring boot·mysql·充电桩管理系统
茉莉玫瑰花茶3 小时前
GO [ 方法 ]
开发语言·后端·golang
狗凯之家源码网4 小时前
微博图床 PHP 系统搭建与实战应用指南
android·开发语言·php
自强的小白4 小时前
核心功能(Service接口)
java·mybatis