POI 操作 Excel 中 HSSFCell.CELL_TYPE_STRING 等无定义解决方法

针对Apache POI jar包版本更新导致的代码修改问题,本文介绍如何通过引入新的CellType包并调整代码来实现对不同类型的Excel单元格数据进行正确处理。包括数字、布尔值、错误值、公式和字符串等。

一、错误原因:jar包版本更新,官方改动;
二、解决方案:导入CellType包import org.apache.poi.ss.usermodel.CellType;使用CellType.STRING代替HSSFCell.CELL_TYPE_STRING
三、旧版代码:
/**
     * excel值处理
     * @param hssfCell
     * @return
     */
    public static Object getXSSFValue(XSSFCell hssfCell) {
        if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
            return hssfCell.getNumericCellValue();    //数字
        }else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
            return hssfCell.getBooleanCellValue();    //boolean
        }else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_ERROR){
            return hssfCell.getErrorCellValue();      //故障
        }else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_FORMULA){
            return hssfCell.getCellFormula();         //公式
        }else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
            return "";                                //空值
        }else {
            return hssfCell.getStringCellValue();     //字符串
        }
        
    }
    
    /**
     * excel值处理
     * @param hssfCell
     * @return
     */
    public static Object getValue(Cell hssfCell) {
        if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC) {
            return hssfCell.getNumericCellValue();   //数字 
        }else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_BOOLEAN) {
            return hssfCell.getBooleanCellValue();   //boolean
        }else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_ERROR){
            return hssfCell.getErrorCellValue();     //故障
        }else if(hssfCell.getCellType()==XSSFCell.CELL_TYPE_FORMULA){
            return hssfCell.getCellFormula();        //公式
        }else if(hssfCell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
            return "";                                  //空值
        }else {
            return hssfCell.getStringCellValue();    //字符串
        }
    }
四、新版代码:
/**
     * excel值处理
     *
     * @param hssfCell
     * @return
     */
    public static Object getXSSFValue(XSSFCell hssfCell) {
        Object result = null;
        CellType cellType = hssfCell.getCellType();
        switch (hssfCell.getCellType()) {
            case NUMERIC: //数字
                result = hssfCell.getNumericCellValue();
                break;
            case BOOLEAN: //Boolean
                result = hssfCell.getBooleanCellValue();
                break;
            case ERROR: //故障
                result = hssfCell.getErrorCellValue();
                break;
            case FORMULA: //公式
                result = hssfCell.getCellFormula();
                break;
            case BLANK: //空值
                result = "";
                break;
            default: //字符串
                result = hssfCell.getStringCellValue();
        }
        return result;
    }
 
    /**
     * excel值处理
     *
     * @param hssfCell
     * @return
     */
    public static Object getValue(Cell hssfCell) {
        Object result = null;
        switch (hssfCell.getCellType()) {
            case NUMERIC: //数字
                result = hssfCell.getNumericCellValue();
                break;
            case BOOLEAN: //Boolean
                result = hssfCell.getBooleanCellValue();
                break;
            case ERROR: //故障
                result = hssfCell.getErrorCellValue();
                break;
            case FORMULA: //公式
                result = hssfCell.getCellFormula();
                break;
            case BLANK: //空值
                result = "";
                break;
            default: //字符串
                result = hssfCell.getStringCellValue();
        }
        return result;
    }
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!

文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「java·D·wj」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wang_jing_jing/article/details/118933708

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值