当前位置: 首页 > news >正文

easyexcel常见问题分析

文章目录

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


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

浮点型转成BigDecimal的时候会出现精度问题,例如
在这里插入图片描述
这儿设置的实体类对象类型是String,默认用到的是StringNumberConverter转换器
2.1.4 版本

public class StringNumberConverter implements Converter<String> {@Overridepublic Class supportJavaTypeKey() {return String.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.NUMBER;}@Overridepublic String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {// If there are "DateTimeFormat", read as dateif (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 numberif (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {return NumberUtils.format(cellData.getNumberValue(), contentProperty);}// Excel defines formattingif (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 numberreturn NumberUtils.format(cellData.getNumberValue(), contentProperty);}@Overridepublic CellData convertToExcelData(String value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return new CellData(new BigDecimal(value));}
}

2.2.8 版本

public class StringNumberConverter implements Converter<String> {@Overridepublic Class supportJavaTypeKey() {return String.class;}@Overridepublic CellDataTypeEnum supportExcelTypeKey() {return CellDataTypeEnum.NUMBER;}@Overridepublic String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {// If there are "DateTimeFormat", read as dateif (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 numberif (contentProperty != null && contentProperty.getNumberFormatProperty() != null) {return NumberUtils.format(cellData.getNumberValue(), contentProperty);}// Excel defines formattingif (cellData.getDataFormat() != null && !StringUtils.isEmpty(cellData.getDataFormatString())) {// 直接返回doubleValue,对弈的类型为double,转成String不会出现精度问题return NumberDataFormatterUtils.format(cellData.getNumberValue().doubleValue(), cellData.getDataFormat(),cellData.getDataFormatString(), globalConfiguration);}// Default conversion numberreturn NumberUtils.format(cellData.getNumberValue(), contentProperty);}@Overridepublic CellData convertToExcelData(String value, ExcelContentProperty contentProperty,GlobalConfiguration globalConfiguration) {return new CellData(new BigDecimal(value));}
}

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

http://www.lryc.cn/news/448509.html

相关文章:

  • 精通推荐算法31:行为序列建模之ETA — 基于SimHash实现检索索引在线化
  • Python知识点:如何使用Python进行卫星数据分析
  • Python实现Phong着色模型算法
  • 异步框架 fastapi -- 连接mysql数据库
  • Spring 全家桶使用教程 —— 后端开发从入门到精通
  • AI动漫转真人终极教程!3步做出爆款内容,音乐推广号变现
  • vue2 vconsole有助于移动端开发页面调试
  • 别再使用[]来获取字典的值了,来尝试一下这些方法
  • 如果你不愿意冒一切风险,就不要成为创业者:如何建立一个年收入 1800 万美元的支付业务
  • 4.浮点数二分【求数的平方根】
  • 简站wordpress主题产品多图ACF插件设置方法
  • USB设备在Linux系统中的识别和加载过程
  • nacos通过@Value动态刷新配置
  • [研发工具箱] 系列3.机电类常用的分类网站
  • volatile关键字最全原理剖析
  • mysql学习教程,从入门到精通,SQL RIGHT JOIN语句(24)
  • LeaferJS 动画、状态、过渡、游戏框架
  • 14年408-计算机网络
  • 告别熬夜,追求高效写作:芝士AI写作,效率与质量的双重提升
  • stm32单片机个人学习笔记8(TIM输出比较)
  • 【qt】QQ仿真项目1
  • Vue3:shallowRef与shallowReactive
  • django开发流程3(轮播图)
  • MySQL的增删查改(基础)一
  • 深度学习(入门)03:监督学习
  • Django——admin创建和使用
  • 鸿蒙开发(NEXT/API 12)【硬件(取消注册智慧出行连接状态的监听)】车载系统
  • JVM中的GC流程与对象晋升机制详解
  • SQL:如果字段需要排除某个值但又有空值时,不能直接用“<>”或not in
  • 运放模块的选型参数