使用 POI HSSF API 从 Excel 单元格读取日期值 [英] Reading date values from excel cell using POI HSSF API
问题描述
我在 Java 中使用 POI HSSF API 进行 excel 操作.我的一个 Excel 单元格中有一个日期值8/1/2009",当我尝试使用 HSSF API 读取该值时,它检测到单元格类型为数字并返回日期的双倍"值.请参阅下面的示例代码:
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屋!