java poi 獲取單元格值時間


完整幫助類:JAVA poi 幫助類

    /*
     * poi特殊日期格式:數字格式化成-yyyy年MM月dd日,格式
     * */
    private static ArrayList<String> PoiDateList = new ArrayList<String>() {
        {
            add("");
            add("");
            add("");
        }
    };

    /// <summary>
    /// 獲取XSSFRow的值(全部統一轉成字符串)
    /// </summary>
    /// <param name="row"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static String GetValue(Row row, int index) {
        Cell rowCell = row.getCell(index);
        return rowCell == null ? "" : GetValueByCellStyle(rowCell, rowCell.getCellType());
    }

    /// <summary>
    /// 根據單元格的類型獲取單元格的值
    /// </summary>
    /// <param name="rowCell"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public static String GetValueByCellStyle(Cell rowCell, int rowCellType) {
        String value = "";
        switch (rowCellType) {
            case Cell.CELL_TYPE_STRING:
                value = rowCell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                //  獲取單元格值的格式化信息
                String dataFormat = rowCell.getCellStyle().getDataFormatString();
                //  判斷格式化信息中是否存在:年月日
                AtomicReference<Boolean> isDate = new AtomicReference<>(false);
                if (!StringHelper.IsNullOrWhiteSpace(dataFormat))
                    PoiDateList.forEach(x -> isDate.set(isDate.get() || dataFormat.contains(x)));

                if (DateUtil.isCellDateFormatted(rowCell)) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                } else if (DateUtil.isCellInternalDateFormatted(rowCell)) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                }
                //有些情況,時間搓?數字格式化顯示為時間,不屬於上面兩種時間格式
                else if (isDate.get()) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(rowCell.getDateCellValue());
                }
                //有些情況,時間搓?數字格式化顯示為時間,不屬於上面兩種時間格式
                else if (dataFormat == null) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(rowCell.getNumericCellValue()));
                } else {
                    if (StringHelper.IsNullOrWhiteSpace(dataFormat)) {
                        value = String.valueOf(rowCell.getNumericCellValue());
                    } else {
                        if (rowCell.getCellStyle().getDataFormatString().contains("$")) {
                            value = "$" + rowCell.getNumericCellValue();
                        } else if (rowCell.getCellStyle().getDataFormatString().contains("")) {
                            value = "" + rowCell.getNumericCellValue();
                        } else if (rowCell.getCellStyle().getDataFormatString().contains("¥")) {
                            value = "¥" + rowCell.getNumericCellValue();
                        } else if (rowCell.getCellStyle().getDataFormatString().contains("")) {
                            value = "" + String.valueOf(rowCell.getNumericCellValue());
                        } else {
                            value = String.valueOf(rowCell.getNumericCellValue());
                        }
                    }
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(rowCell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                value = ErrorEval.getText(rowCell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                //  TODO: 是否存在 嵌套 公式類型
                value = GetValueByCellStyle(rowCell, rowCell.getCachedFormulaResultType());
                break;
            default:
                System.out.println(rowCell);
                break;
        }
        return value;
    }

 


免責聲明!

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



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