• Excel导入异常Cannot get a STRING value from a NUMERIC cell解决


    报错

    导入Excel文件出现异常
    Cannot get a STRING value from a NUMERIC cell
    报错代码

    String value = row.getCell(column).getStringCellValue();
    
    • 1

    Excel依赖

    <dependency>
        <groupId>org.apache.poigroupId>
        <artifactId>poi-ooxmlartifactId>
        <version>4.1.2version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    解决

    方案一

    适用于POI 5.0以前,5.0版本后setCellType方法会被废弃。

    Cell value = row.getCell(column);
    if (StringUtils.isNotNull(value)) {
        value.setCellType(CellType.STRING);
        System.out.println(value.getStringCellValue());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方案二(推荐)

    用于多种类型处理
    先判断单元格类型,在根据类型获取值,这样就万无一失了

    Cell cell = row.getCell(column);
    if (StringUtils.isNotNull(cell))
    {
        if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA)
        {
            val = cell.getNumericCellValue();
            if (DateUtil.isCellDateFormatted(cell))
            {
             	 // POI Excel 日期格式转换
                val = DateUtil.getJavaDate((Double) val);
            }
            else
            {
                if ((Double) val % 1 != 0)
                {
                    val = new BigDecimal(val.toString());
                }
                else
                {
                    val = new DecimalFormat("0").format(val);
                }
            }
        }
        else if (cell.getCellType() == CellType.STRING)
        {
            val = cell.getStringCellValue();
        }
        else if (cell.getCellType() == CellType.BOOLEAN)
        {
            val = cell.getBooleanCellValue();
        }
        else if (cell.getCellType() == CellType.ERROR)
        {
            val = cell.getErrorCellValue();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    复盘

    报错源码信息提示:

    Cannot get a STRING value from a NUMERIC cell
    org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1035)
    org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:390)
    org.apache.poi.xssf.usermodel.XSSFCell.getStringCellValue(XSSFCell.java:342)
    
    • 1
    • 2
    • 3
    • 4

    相关源码

    /**
     * Used to help format error messages
     */
    private static RuntimeException typeMismatch(CellType expectedType, CellType actualType, boolean isFormulaCell) {
        String msg = "Cannot get a " + expectedType + " value from a " + actualType+ " " + (isFormulaCell ? "formula " : "") + "cell";
        return new IllegalStateException(msg);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    
    @Deprecated
    @Removal(version = "5.0")
    void setCellType(CellType cellType);
    
    • 1
    • 2
    • 3
    • 4

    扩展

    在这里插入图片描述
    Cell类的这三个实现有什么区别呢

    HSSF:适用于Excel97-2003版本,07版本之前默认扩展名为.xls。
    XSSF:适用于Excel2007及之后的版本,默认扩展名为.xlsx,向下兼容。
    SXSSF:是在XSSF基础上,POI3.8版本开始提供的一种支持低内存占用的操作方式,扩展名为.xlsx。
    XLSX格式的占用空间比XLS的小。XLSX是用新的基于XML的压缩文件格式取代了XLS默认文件格式。

    台下人走过 不见旧颜色 台上人唱着 心碎离别歌
    在这里插入图片描述

  • 相关阅读:
    基于模糊BP神经网络轨迹跟踪(Matlab代码实现)
    Java web基础知识
    多疑型性格的危害,如何改变多疑型性格?
    LeetCode--578. 查询回答率最高的问题
    CUDA 中的线程组织
    SpringCloud03 --- 搭建Nacos集群、Feign远程调用、Gateway服务网关
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java智慧社区家政服务系统80q7o
    【SwiftUI模块】0005、SwiftUI-粘性动画指示器引导页
    1110 区块反转 – PAT乙级真题
    java版直播商城免费搭建平台规划及常见的营销模式+电商源码+小程序+三级分销+二次开发
  • 原文地址:https://blog.csdn.net/qq_35764295/article/details/126030573