从Excel单元格中读取日期值作为字符串 [英] Reading date value from excel cell as a String

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

问题描述

我使用的Apache POI库来读取Excel文件。我在阅读密码卡电池。如果用户给的日期作为密码,即细胞一16/05/2012密码。我读这个数值为41045,而价值应该是16/05/2012。这是我的code:

I am using apache poi library to read excel file. I am stuck while reading password cell. If user gives date as a password in password cell i.e. 16/05/2012. I am reading this value as "41045" while value should be "16/05/2012". This is my code :

cell = row.getCell(); // date in the cell password '16/05/2012'
switch (cell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:
    cellValue = cell.getRichStringCellValue().getString();
    break;
case HSSFCell.CELL_TYPE_NUMERIC:
    if(cellCount == TEMPLATE_PASSWORD) {// if cell is password
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cellValue = cell.getRichStringCellValue().getString(); // value read is 41045 and not "16/05/2012"
    }
    break;
default:
}

任何人都可以在帮助呢?

can anyone help on this?

感谢。

推荐答案

你的POI寻找的类是<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html\">DataFormatter

在Excel中写到文件中,部分细胞被存储为文本字符串,而其他存储为数字(包括日期)。对于后者,浮点值重新presenting细胞被保存在文件中,所以当你问POI的单元格的值,这就是它实际上有。

When Excel writes the file, some cells are stored as literal Strings, while others are stored as numbers (including dates). For the latter, a floating point value representing the cell is stored in the file, so when you ask POI for the value of the cell that's what it actually has.

虽然有时,特别是在做文本提取(但不总是)时,你想使单元格的值看起来像它在Excel中一样。它并不总是能够得到准确的字符串(非全空间填充为例),但DataFormatter类将让你关闭。

Sometimes though, especially when doing Text Extraction (but not always), you want to make the cell value look like it does in Excel. It isn't always possible to get that exactly in a String (non full space padding for example), but the DataFormatter class will get you close.

另外请注意,在Excel中,日期被存储为浮点数自1900〜,这就是为什么你看到一个数字而不是日期。使用DataFormatter(或类似)将它呈现为一个日期

Also note that in Excel, dates are stored as floating point numbers since ~1900, which is why you're seeing a number not a date. Use DataFormatter (or similar) to have it rendered as a date

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

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