excel学习库

excel表格_excel函数公式大全_execl从入门到精通

Apache POI 读取excel数据格式转换

今日份鸡汤:不完美又何妨,万物皆有裂痕,那是光进来的地方~

问题描述:

我使用Apache POI读取excel表格,其中有一列数值为:1215515164,但是我Java后端debug的时候,看到解析出来的数据值为:1.215515164E9,而我的需求其实就是获取到 1215515164 这个值,那就需要想一下:有没有办法从原始Cell中获取值作为字符串?

解决办法:

使用BigDecimal的toPlainString()方法,它采用科学记数法并将其转换为相应的数字字符串值。

代码演示:

Row row = sheet.getRow(0);Object o = getCellValue(row.getCell(0));System.out.println(new BigDecimal(o.toString()).toPlainString());
public Object getCellValue(Cell cell) {        if (cell != null) {            switch (cell.getCellType()) {            case STRING:                return cell.getStringCellValue();            case BOOLEAN:                return cell.getBooleanCellValue();            case NUMERIC:                return cell.getNumericCellValue();            case ERROR:                return cell.getErrorCellValue();            }        }        return null;    }

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年12月    »
1
2345678
9101112131415
16171819202122
23242526272829
3031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
      友情链接