2017-06-22 15 views
0

通常のセル値ですべて正常に動作することをExcelファイルから読み取ろうとしていました。しかし、式、特に日付フィールドに来ると、私は日付の値を取得することができません。私が評価CellValueにdate型を確認することができますどのようにApache POIを使用してExcelから日付公式のセルの日付値を取得するJava

Date generated using formula in excel

?日付の価値を得る方法

私のコードは以下の通りです。

Cell cell = nextRow.getCell(cellIndex, Row.CREATE_NULL_AS_BLANK); 
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { 
    FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator(); 
    CellValue formulaValue = evaluator.evaluate(cell); 
    switch (formulaValue.getCellType()) { 
     case Cell.CELL_TYPE_NUMERIC: { 
      int numbericValue = formulaValue.getNumericValue(); // 42909.0 
      if (isCellDate?) { //What should i check here? 
       // DATE THINGS 
       // how to convert numeric value 42909.0 to date?? 
      } else { 
       // NUMERIC THINGS 
      } 
      break; 
     } 
     default: 
      // STRING - ERROR - BLANK - NUMERIC THINGS 
      break; 
    } 
} 

答えて

1
HSSFDateUtil : Helped to solve my issue. my solution is : 

case Cell.CELL_TYPE_NUMERIC: { 
if (HSSFDateUtil.isCellDateFormatted(cell)) { 
    Date date = HSSFDateUtil.getJavaDate(formulaValue.getNumberValue()); 
    System.out.println(date); 
} else { 
    System.out.println(cell.getNumericCellValue()); 
} 
break; 

}

+1

https://poi.apache.org/spreadsheet/quick-guide.html#CellContents –

関連する問題