EXCEl中关于Cell中的各种值的类型判断


    private static String getCellValue(XSSFCell cell)
    {
//        DecimalFormat df = new DecimalFormat("#.#");
        String cellValue = null;
        if (cell == null) return null;
        switch (cell.getCellType())
        {
            case HSSFCell.CELL_TYPE_NUMERIC :
                if (HSSFDateUtil.isCellDateFormatted(cell))
                {
//                    需要对日期这一列进行设置样式,否则无法识别是日期类型还是数值类型
//                    默认不支持中文日期类型,需要设置成纯英语日期类型,不要包含年月日等汉字
//                    最好是使用这种格式 2019/10/10 0:00
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "yyyy-MM-dd HH:mm:ss");
                    cellValue = sdf.format(HSSFDateUtil.getJavaDate(cell
                            .getNumericCellValue()));
                    break;
                }
//                cellValue = df.format(cell.getNumericCellValue());
                String v=String.format("%.4f",cell.getNumericCellValue());      //默认的整数后边是一个小数点,和一个零
                cellValue=(v).replaceAll("\\.0*$","");//整数作为浮点数格式化以后,删除结尾的点零
                break;
            case HSSFCell.CELL_TYPE_STRING :
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case HSSFCell.CELL_TYPE_FORMULA ://XSSFCell.getCTCell()
                cellValue = cell.getCTCell().getV();//.getCachedFormulaResultType();//String.valueOf(cell..getCellFormula());
                break;
            case HSSFCell.CELL_TYPE_BLANK :
                cellValue = null;
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN :
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case HSSFCell.CELL_TYPE_ERROR :
                cellValue = String.valueOf(cell.getErrorCellValue());
                break;
        }
        if (cellValue != null && cellValue.trim().length() <= 0)
        {
            cellValue = null;
        }
        return cellValue;
    }

switch (cell.getCellType()){
    case Cell.CELL_TYPE_NUMERIC: //数字
        cellValue = stringDateProcess(cell);
        break;
    case Cell.CELL_TYPE_STRING: //字符串
        cellValue = String.valueOf(cell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_BOOLEAN: //Boolean
        cellValue = String.valueOf(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA: //公式
        cellValue = String.valueOf(cell.getCellFormula());
        break;
    case Cell.CELL_TYPE_BLANK: //空值
        cellValue = "";
        break;
    case Cell.CELL_TYPE_ERROR: //故障
        cellValue = "非法字符";
        break;
    default:
        cellValue = "未知类型";
        break;
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值