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 ,解决读取不准确的问题

相关推荐
java—大象8 分钟前
基于Java+Jsp+SpringMVC漫威手办商城系统设计和实现
java·数据库·spring boot·python·课程设计
蔚然丶丶1 小时前
Excel根据一个值匹配一行数据
excel
IT学长编程2 小时前
计算机毕业设计 校运会管理系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·校园会管理系统
无惧代码2 小时前
实现quartz定时任务控制是否启动 (生产环境 和 开发环境)
java·spring boot·spring
小旋风-java2 小时前
springboot整合dwr
java·spring boot·后端·dwr
听潮阁3 小时前
【SpringBoot详细教程】-05-整合Druid操作数据库【持续更新】
数据库·spring boot·后端
gobeyye3 小时前
SpringBoot | Maven快速上手
spring boot·后端·maven
开源哥663 小时前
基于Springboot+微信小程序 的高校社团管理小程序(含源码+数据库+lw)
spring boot·微信小程序·小程序
2401_857617623 小时前
Spring Boot电商开发:购物商城系统
java·spring boot·后端
丁总学Java4 小时前
dockerfile部署springboot项目(构建镜像:ebuy-docker:v1.0)
java·spring boot·后端·dockerfile