问题描述
我正在使用 Apache POI 3.6,我想读取一个具有类似 8/23/1991
日期的 excel 文件.
I'm using Apache POI 3.6, I want to read an excel file which has a date like this 8/23/1991
.
switch (cell.getCellType()) {
...
...
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue();
break;
...
}
但它采用数值类型并返回这样的值33473.0
.
But it takes the numeric value type and returns the value like this 33473.0
.
我尝试使用数字单元格类型,但没有成功.
I've tried to use Numeric Cell Type although with no luck.
dbltemp=row.getCell(c, Row.CREATE_NULL_AS_BLANK).getNumericCellValue();
if (c == 6 || c == 9) {
strTemp= new String(dbltemp.toString().trim());
long tempDate = Long.parseLong(strTemp);
Date date = new Date(tempDate);
strVal = date.toString();
}
我该如何解决我的问题?
How can I fix my problem?
推荐答案
注意:HSSFDateUtil 已弃用
如果你知道哪个单元格,即列位置说每行中的 0 将是一个日期,你可以去row.getCell(0).getDateCellValue()
直接.
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getDateCellValue()
If you know which cell i.e. column position say 0 in each row is going to be a date, you can go forrow.getCell(0).getDateCellValue()
directly.
http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFCell.html#getDateCellValue()
更新:这是一个示例 - 您可以在上面的 switch case 代码中应用它.我正在检查和打印数字以及日期值.在这种情况下,我的工作表中的第一列有日期,因此我使用 row.getCell(0).
UPDATE: Here is an example - you can apply this in your switch case code above. I am checking and printing the Numeric as well as Date value. In this case the first column in my sheet has dates, hence I use row.getCell(0).
您可以在 switch case 中直接使用 if (HSSFDateUtil.isCellDateFormatted ..
代码块.
You can use the if (HSSFDateUtil.isCellDateFormatted ..
code block directly in your switch case.
if (row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
System.out.println ("Row No.: " + row.getRowNum ()+ " " +
row.getCell(0).getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(row.getCell(0))) {
System.out.println ("Row No.: " + row.getRowNum ()+ " " +
row.getCell(0).getDateCellValue());
}
}
输出为
Row No.: 0 39281.0
Row No.: 0 Wed Jul 18 00:00:00 IST 2007
Row No.: 1 39491.0
Row No.: 1 Wed Feb 13 00:00:00 IST 2008
Row No.: 2 39311.0
Row No.: 2 Fri Aug 17 00:00:00 IST 2007
这篇关于如何使用Apache POI读取具有日期的Excel单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!