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;
                }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM