excel判断单元格类型获取内容

          switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC: // 数字
                    if (DateUtil.isCellDateFormatted(cell)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        try {
                            cellValue = sdf.format(cell.getDateCellValue());// 日期
                        } catch (Exception e) {
                            throw new Exception("exception on get date data !".concat(e.toString()));
                        }finally{
                            sdf = null;
                        }
                    } else {
                        BigDecimal bd = new BigDecimal(cell.getNumericCellValue()); 
                        cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计数值
                    }
                    break;
                case Cell.CELL_TYPE_STRING: // 字符串
                    cellValue = getString(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN: // Boolean
                    cellValue = getString(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA: // 公式
                    cellValue = getString(cell.getCellFormula());
                    break;
                case Cell.CELL_TYPE_BLANK: // 空值
                    cellValue = "";
                    break;
                case Cell.CELL_TYPE_ERROR: // 故障
                    cellValue = "ERROR VALUE";
                    break;
                default:
                    cellValue = "UNKNOW VALUE";
                    break;
                }

 

posted @ 2018-05-25 14:03  稚语希听  阅读(2053)  评论(0)    收藏  举报