简体   繁体   中英

Java - Apache POI HSSF/XSSF - Trouble reading cell with data format XX:XX

So here is the issue, I am atempting to use the Apace POI libraries to read an excel file with some data in the following format:


Tag    |    Date    |  Hour | Value
X      | 20150101   | 00:00 | 15
X      | 20150101   | 00:15 | 16
X      | 20150101   | 00:30 | 20

So as you can imagine the columns Tag, Date and Value are preaty easy to obtain. My issue is with the Hour column, being that its somehow classified as a numerical value. I have atempted to turn it into a String using cell.setCellType(CELL_TYPE_STRING); however it still returns some sort of number I cannot make sense of the returned values.

Namely they return as follows:

00:00 --> 0
00:15 --> 1.04166666666666E-2
00:30 --> 2.08333333333332E-2
and so on...

The wierd thing is that tjese numbers behave in a awkward way, after the day reaches its end, at 23:45, the numbers do go down but not to the 0 value, they go a little bit up, by the end of the record list the value returned is 299.98958333336702.

If someone could help me to properly obtain this value I would apreciate it. Below is the source code I am running:

 Workbook wb = null;
    try{
        wb = WorkbookFactory.create(p_file);
    }catch(IOException | InvalidFormatException e){}

    Sheet sheet = wb.getSheetAt(0);

    Cell c;
    int x,y,z;
    String hour;
    for(x = 0; x <  sheet.getLastRowNum(); x++){
        for(y = 0; y < 4; y++){
            c = sheet.getRow(x).getCell(y);
            if(x == 0){
                System.out.print(c.getStringCellValue()+ "|");
            }else{
                switch(y){
                    case 0:
                        System.out.print(c.getStringCellValue()+ "|");
                        break;
                    case 1:
                        z = (int) c.getNumericCellValue();
                        int offset1 = z/10000;
                        int offset2 = z/100-offset1*100;
                        int offset3 = z-offset2*100-offset1*10000;
                        System.out.print(offset1 +"/"+offset2+"/"+offset3+ "|");
                        break;
                    case 2:
                        c.setCellType(CELL_TYPE_STRING);
                        hour = c.getStringCellValue();
                        System.out.print(hour);
                        break;
                    case 3:
                        break;
                }
            }
        }
        System.out.println("");

I know some code is missing, particularly in case 3. The code in case 2 is my atempt described above.

As per my understanding the return value what you are getting is timestamp value.

A Timestamp, Unix time, or POSIX time, is a system for describing points in time, defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds. It is widely used not only on Unix-like operating systems but also in many other computing systems. It is neither a linear representation of time nor a true representation of UTC (though it is frequently mistaken for both) as the times it represents are UTC but it has no way of representing UTC leap seconds (eg 1998-12-31 23:59:60).

There are many more online website available where you can convert date/time to timestamp and vice versa. You can verify from that.

For the solution you should use dataFormat to get the desired format. One example is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM