美文网首页
Apache POI 读取excel数据格式转换

Apache POI 读取excel数据格式转换

作者: wyn_做自己 | 来源:发表于2022-05-18 14:34 被阅读0次

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

问题描述:

我使用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;
    }

相关文章

网友评论

      本文标题:Apache POI 读取excel数据格式转换

      本文链接:https://www.haomeiwen.com/subject/vmraurtx.html