从Excel单元格使用POI HSSF API读取日期值 [英] Reading date values from excel cell using POI HSSF API

查看:1058
本文介绍了从Excel单元格使用POI HSSF API读取日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用POI HSSF API在Java我的excel操作。我在Excel单元格中的一个已经的日期值2009年8月1日,虽然我尝试阅读使用HSSF API此值,检测的细胞类型为数字,并返回我的约会的双师型的价值。请参见下面的示例code:

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:

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()返回NUMERIC_TYPE,因此这code转换为DOUBLE日期! (

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

有什么办法来读取日期,因为它是在POI HSSF!?

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

推荐答案

您可以看看:

HSSFDateUtil.isCellDateFormatted()

查看POI可怕的小号preadsheet格式的更多详细信息的API上HSSFDateUtil:

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

<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html\">http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html

这也提供了Excel中返回一些辅助方法 getExcelDate()和Java日期 getJavaDate()。你需要有所警惕不同的日期格式,虽然...

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...

这篇关于从Excel单元格使用POI HSSF API读取日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆