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

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

问题描述

我正在使用POI HSSF API来进行Java中的excel操作。我的一个excel单元格中有一个日期值8/1/2009,当我尝试使用HSSF API读取这个值时,它会将单元格类型检测为Numeric,并返回我的日期的Double值。请参阅下面的示例代码:

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,代码将日期转换为双倍! :(

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

有没有办法读取HSSF POI中的日期?

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

推荐答案

您可以看一下:

HSSFDateUtil.isCellDateFormatted()

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

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

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

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

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