Java - Apache POI HSSF / XSSF - 无法读取数据格式为XX:XX的单元格

时间:2015-04-14 11:38:51

标签: java excel apache-poi

所以这就是问题,我试图使用Apace POI库来读取包含以下格式的一些数据的excel文件:


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

因此,您可以想象标签,日期和价值列很容易获得。 我的问题是关于小时列,因为它以某种方式归类为数值。我尝试使用 cell.setCellType(CELL_TYPE_STRING); 将其转换为字符串但是它仍然返回某种我无法理解返回值的数字。

即他们返回如下:

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

奇怪的是,这些数字表现得很尴尬,在一天结束后,在23:45,数字确实下降但没有达到0值,它们有点上升,到最后记录列表中返回的值是 299.98958333336702。

如果有人能帮助我正确获得这个价值,我会贬低它。 以下是我正在运行的源代码:

 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("");

我知道有些代码丢失了,特别是在案例3中。案例2中的代码是我上面描述的尝试。

1 个答案:

答案 0 :(得分:0)

根据我的理解,您获得的返回值是时间戳值。

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 (e.g. 1998-12-31 23:59:60).

还有更多在线网站,您可以将日期/时间转换为时间戳,反之亦然。你可以从那里验证。

对于解决方案,您应该使用dataFormat来获取所需的格式。一个例子是here