简体   繁体   English

使用Apache POI Java从Excel获取具有日期公式的单元格的Date值

[英]Get Date value of cell with date formula from Excel using Apache POI Java

I was trying to read from excel file everything work good with normal cell values. 我试图从excel文件中读取所有正常工作与正常的单元格值。 But when came to formulas, especially Date fields, I am not able to get any Date values. 但是当涉及到公式,尤其是日期字段时,我无法获取任何日期值。

在Excel中使用公式生成的日期

How can i check for date type in Evaluated CellValue?? 我该如何检查Evaluated CellValue中的日期类型? and how to get date value? 以及如何获取日期值?

My code is given below. 我的代码如下。

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM