Java 使用 POI HSSF API 从 Excel 单元格读取日期值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/861877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:21:45  来源:igfitidea点击:

Reading date values from excel cell using POI HSSF API

javaexcelapache-poipoi-hssf

提问by Veera

I'm using POI HSSF API for my excel manipulations in Java. I've a date value "8/1/2009" in one of my excel cell and while I try to read this value using HSSF API, it detects the cell type as Numeric and returns the 'Double' value of my date. See the sample code below:

我在 Java 中使用 POI HSSF API 进行 excel 操作。我的一个 Excel 单元格中有一个日期值“8/1/2009”,当我尝试使用 HSSF API 读取该值时,它检测到单元格类型为数字并返回日期的“双倍”值。请参阅下面的示例代码:

cell = row.getCell(); // date in the cell '8/1/2009'
switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:
    cellValue = cell.getRichStringCellValue().getString();
    break;
case HSSFCell.CELL_TYPE_NUMERIC:
    cellValue = new Double(cell.getNumericCellValue()).toString();
    break;
default:
}

Cell.getCellType() returns NUMERIC_TYPE and thus this code converts the date to double! :(

Cell.getCellType() 返回 NUMERIC_TYPE,因此此代码将日期转换为双精度!:(

Is there any way to read the date as it is in HSSF POI !?

有什么方法可以读取 HSSF POI 中的日期!?

采纳答案by Jon

You could take a look at:

你可以看看:

HSSFDateUtil.isCellDateFormatted()

See the POI Horrible Spreadsheet Format API for more details on HSSFDateUtil:

有关 HSSFDateUtil 的更多详细信息,请参阅 POI Horrible Spreadsheet Format API:

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html

That also provides some helper methods for returning Excel getExcelDate()and Java dates getJavaDate(). You need to be somewhat wary of different date formats though...

这也提供了一些用于返回 ExcelgetExcelDate()和 Java 日期的辅助方法getJavaDate()。不过,您需要对不同的日期格式有所警惕...

回答by Stobor

Excel treats dates and times as numbers... Jon said it better, so I won't echo him here...

Excel 将日期和时间视为数字......乔恩说得更好,所以我不会在这里重复他......

However, sample code for what you've put in the question is at http://poi.apache.org/spreadsheet/quick-guide.html#CellContents

但是,您在问题中提出的示例代码位于http://poi.apache.org/spreadsheet/quick-guide.html#CellContents

回答by Naveen

If you using the POI 3.5 you can use the following

如果您使用 POI 3.5,则可以使用以下内容

cell.getDateCellValue() method. This will work for excel 2007 as well.

cell.getDateCellValue() 方法。这也适用于 excel 2007。

回答by Sayantam

If you want to reference the date in the same format in as in the Excel file, you should use the CellDateFormatter. Sample code:

如果要以与 Excel 文件中相同的格式引用日期,则应使用 CellDateFormatter。示例代码:

CellValue cValue = formulaEv.evaluate(cell);
double dv = cValue.getNumberValue();
if (HSSFDateUtil.isCellDateFormatted(cell)) {
    Date date = HSSFDateUtil.getJavaDate(dv);

    String dateFmt = cell.getCellStyle().getDataFormatString();
    /* strValue = new SimpleDateFormat(dateFmt).format(date); - won't work as 
    Java fmt differs from Excel fmt. If Excel date format is mm/dd/yyyy, Java 
    will always be 00 for date since "m" is minutes of the hour.*/

    strValue = new CellDateFormatter(dateFmt).format(date); 
    // takes care of idiosyncrasies of Excel
}

回答by Micha? Pi?tkowski

Since POI 3.15 beta3 some functions are deprecated. You can check data format and retrieve as Java Date.

从 POI 3.15 beta3 开始,一些函数是deprecated. 您可以检查数据格式并检索为 Java Date

SimpleDateFormat format = new SimpleDateFormat("");
String cellValue;
if(cell != null && cell.getCellTypeEnum() != CellType.STRING &&
    cell.getCellTypeEnum() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)){

    cellValue = format.format(cell.getDateCellValue());
}