POI導入Excel,獲取公式的值

直接POI導入Excel中的數據的時候,直接獲取表中的值,如果表中單元格的值時由公式計算得出的話,獲取到的會是公式

所以需要對獲取的單元格的值進行處理:

   /**
     * 導入數字時。導入公式的計算結果而非公式
     * @param cell
     * @return
     */
    public static String getCellValue(XSSFCell cell) {
        CellType cellType = cell.getCellTypeEnum();
        String cellValue = "";
        switch (cellType) {
            case NUMERIC:
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;

            case FORMULA:
                try {
                    cellValue = cell.getStringCellValue();
                } catch (IllegalStateException e) {
                    cellValue = String.valueOf(cell.getNumericCellValue());
                }
                break;

            default:
                cellValue = cell.getStringCellValue();
        }


        return cellValue.trim();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章