Java – read date values from excel cells as strings

I use Apache POI library to read Excel files I got stuck reading the password unit If the user enters a date in the password cell, i.e. 16 / 05 / 2012 I am reading this value as "41045", and the 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 I help you?

thank you.

Solution

The course you are looking for in POI is dataformatter

When excel writes to a file, some cells are stored as text strings, while others are stored as numbers, including dates For the latter, the floating point value representing the cell is stored in a file, so when you request the POI for the actual cell value

Sometimes, especially when text extraction (but not always), make the cell value look like in Excel It's not always available in strings (such as incomplete space padding), but the dataformatter class lets you close it

Also note that in Excel, dates are stored as floating-point numbers since 1900, which is why you see a number instead of a date Render it as a date using a dataformatter (or similar)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>