java poi导入纯数字等格式问题及解决

作者:linge__ 时间:2023-04-14 08:50:30 

poi导入纯数字等问题

用poi导出excel时候,如果单元格设置纯数字,输入的数据一旦过大就是自动显示成科学记数法,导致导入后的数据出错,解决方式,后台获取导出文件后,强制转换单元格属性,就能完美解决,也适用于其他单元格格式引起的数据导入异常

Cell cellCode = r.getCell(1);
cellCode.setCellType(CellType.STRING);  
info.setCode(r.getCell(1).getStringCellValue());

poi获取Cell内容:数字之格式化

今天,收到业务方的诉求,说是excel 导入的金额,全被四舍五入了。

然后查看了一下代码:初始的时候眼花缭乱,真的是看不下去。但是想一想,作为程序员,不能拒绝为人民服务。于是乎,debug 了一下。我也真是够懒的,不想直接追代码,debug去了……

原因

找到了具体的原因:

if (xssfRow.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    DecimalFormat format = new DecimalFormat("#");
    double num = xssfRow.getNumericCellValue();
    String res = format.format(num);
    //……
}

上面代码中,把数字格式化为整数了。当然,如果直接获取 value 也不会有问题。

如下:

if (xssfRow.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    DecimalFormat format = new DecimalFormat("#");
    double num = xssfRow.getNumericCellValue();
    String res = format.format(num);
    // num 和 res 的取值差不多。 如: 50.00 : num 为 50.00,res 为 50; 123.23, num 为123.23, res为123.23
    System.err.println(num + "--" + res);
    //……
}

DecimalFormat

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。帮你用最快的速度将数字格式化为你需要的样子。DecimalFormat 包含一个模式 和一组符号 。

DecimalFormat 类主要靠 # 和 0 两种占位符号来指定数字长度。像"####.000"的符号。这个模式意味着在小数点前有四个数字,如果不够就空着,小数点后有三位数字,不足用0补齐。

符号含义: 

  • 0 一个数字 

  • # 一个数字,不包括 0 

  • . 小数的分隔符的占位符 

  • , 分组分隔符的占位符 

  • - 缺省负数前缀。 

  • % 乘以 100 和作为百分比显示  

例子: 

    public static void main(String[] args) {
        double pi=3.1415927;//圆周率
        //取一位整数
        System.out.println(new DecimalFormat("0").format(pi));//3
        //取一位整数和两位小数
        System.out.println(new DecimalFormat("0.00").format(pi));//3.14
        //取两位整数和三位小数,整数不足部分以0填补。
        System.out.println(new DecimalFormat("00.000").format(pi));//03.142
        //取所有整数部分
        System.out.println(new DecimalFormat("#").format(pi));//3
        //以百分比方式计数,并取两位小数
        System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
 
        long c=299792458;//光速
        //显示为科学计数法,并取五位小数
        System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8
        //显示为两位整数的科学计数法,并取四位小数
        System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7
        //每三位以逗号进行分隔。
        System.out.println(new DecimalFormat(",###").format(c));//299,792,458
 
        System.out.println(new DecimalFormat("-###").format(c));//299,792,458
        System.out.println(new DecimalFormat("#.##?").format(c));//299,792,458
        //将格式嵌入文本
        System.out.println(new DecimalFormat("光速大小为每秒,###米").format(c)); //光速大小为每秒299,792,458米
    }

来源:https://blog.csdn.net/weixin_37497666/article/details/78921443

标签:java,poi,导入,纯数字
0
投稿

猜你喜欢

  • C#中利用代理实现观察者设计模式详解

    2022-02-25 17:49:36
  • SpringBoot中自定义注解实现参数非空校验的示例

    2022-04-12 10:55:55
  • Android 在程序运行时申请权限的实例讲解

    2023-08-04 17:35:57
  • Android仿淘宝物流追踪的实例代码

    2021-08-19 22:57:23
  • C#中调用SAPI实现语音识别的2种方法

    2023-01-15 02:19:39
  • C#导入导出Excel数据的两种方法

    2021-07-01 02:29:54
  • Java深入浅出数组的定义与使用上篇

    2022-03-10 22:32:58
  • Android仿百度福袋红包界面

    2023-11-01 04:53:01
  • C#泛型详解及关键字作用

    2023-04-07 20:23:12
  • Android中使用Bitmap类将矩形图片转为圆形的方法

    2022-01-04 18:47:40
  • Android App获取屏幕旋转角度的方法

    2021-12-26 10:42:19
  • springmvc前台向后台传值几种方式总结(从简单到复杂)

    2023-04-13 22:20:50
  • Java实现简单班级管理系统

    2023-01-07 16:20:29
  • Java面试题冲刺第二十六天--实战编程

    2023-05-22 16:01:54
  • Java ArrayList实现班级信息管理系统

    2023-09-17 15:15:25
  • Android编程ProgressBar自定义样式之动画模式实现方法

    2022-02-10 12:54:29
  • Java开发之spring security实现基于MongoDB的认证功能

    2022-06-29 15:52:32
  • java中functional interface的分类和使用详解

    2021-09-15 15:59:20
  • C#从windows剪贴板获取并显示文本内容的方法

    2022-06-03 01:56:47
  • C语言中结构体与内存对齐实例解析

    2022-05-16 12:25:18
  • asp之家 软件编程 m.aspxhome.com