easyexcel常见问题分析

文章目录


一、读取数字多了很多小数位的精度问题

浮点型转成BigDecimal的时候会出现精度问题,例如

这儿设置的实体类对象类型是String,默认用到的是StringNumberConverter转换器

2.1.4 版本

java 复制代码
public class StringNumberConverter implements Converter<String> {

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        // If there are "DateTimeFormat", read as date
        if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
            return DateUtils.format(
                DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
                    contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
                contentProperty.getDateTimeFormatProperty().getFormat());
        }
        // If there are "NumberFormat", read as number
        if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
            return NumberUtils.format(cellData.getNumberValue(), contentProperty);
        }
        // Excel defines formatting
        if (cellData.getDataFormat() != null) {
            if (DateUtil.isADateFormat(cellData.getDataFormat(), cellData.getDataFormatString())) {
                return DateUtils.format(DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
                    globalConfiguration.getUse1904windowing(), null));
            } else {
            	// 直接返回NumberValue,对弈的类型为BigDecimal,因为BigDecimal由double转换而来
            	// 出现了精度读取的问题,所以此时直接读取NumberValue,精度不准确的时候多出很多小数点
            	// 这种情况不是必现的,613999.06是个例子
                return NumberUtils.format(cellData.getNumberValue(), contentProperty);
            }
        }
        // Default conversion number
        return NumberUtils.format(cellData.getNumberValue(), contentProperty);
    }

    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        return new CellData(new BigDecimal(value));
    }
}

2.2.8 版本

java 复制代码
public class StringNumberConverter implements Converter<String> {

    @Override
    public Class supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        // If there are "DateTimeFormat", read as date
        if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
            return DateUtils.format(
                DateUtil.getJavaDate(cellData.getNumberValue().doubleValue(),
                    contentProperty.getDateTimeFormatProperty().getUse1904windowing(), null),
                contentProperty.getDateTimeFormatProperty().getFormat());
        }
        // If there are "NumberFormat", read as number
        if (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {
            return NumberUtils.format(cellData.getNumberValue(), contentProperty);
        }
        // Excel defines formatting
        if (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {
        	// 直接返回doubleValue,对弈的类型为double,转成String不会出现精度问题
            return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),
                cellData.getDataFormatString(), globalConfiguration);
        }
        // Default conversion number
        return NumberUtils.format(cellData.getNumberValue(), contentProperty);
    }

    @Override
    public CellData convertToExcelData(String value, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        return new CellData(new BigDecimal(value));
    }
}

如果无法升级版本,可以重写转换器StringNumberConverter ,解决读取不准确的问题

相关推荐
消失的旧时光-194330 分钟前
Spring Boot 接口设计进阶:POST / PUT / DELETE 的本质区别与工程实践
spring boot·后端
MegaDataFlowers30 分钟前
基于EasyCode插件的SpringBoot和Mybatis框架快速整合以及PostMan的使用
spring boot·mybatis·postman
devilnumber31 分钟前
Spring Boot 2 vs Spring Boot 3:50 条核心区别 + 升级优势 + 避坑指南
java·spring boot·springboot升级
一 乐1 小时前
咖啡商城|基于springboot + vue咖啡商城系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·咖啡商城系统
码农周3 小时前
告别大体积PDF!基于PDFBox的Java压缩工具
java·spring boot
大佬,救命!!!3 小时前
etp中未运行用例顺序的定位及补齐脚本自动化生成
python·学习笔记·excel·自动化脚本·用例整理清洗
专职3 小时前
cursor中与vim插件冲突时的配置
编辑器·vim·excel
吕永强3 小时前
基于SpringBoot+Vue小区报修系统的设计与实现(源码+论文+部署)
spring boot·毕业设计·毕业论文·报修系统·小区报修
程序员老邢5 小时前
【产品底稿 05】商助慧 V1.1 里程碑:RAG 文章仿写模块全链路实现
java·spring boot·程序人生·ai·milvus
消失的旧时光-19435 小时前
Spring Boot 实战(三):Service 分层 + 统一返回 + 异常处理(工程级写法)
java·spring boot·接口·解耦