Java – Apache POI HSSF / XSSF – 数据格式错误读取单元格XX:XX

所以这里是这个问题,我试图使用Apace POI库来读取具有以下格式的一些数据的Excel文件:


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

所以,你可以想象列标签,date和价值preaty容易获得。 我的问题是与小时专栏,是它以某种方式分类为数值。 我试图把它变成一个string使用cell.setCellType(CELL_TYPE_STRING); 但是它仍然返回某种数字,我无法理解返回的值。

即他们返回如下:

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

奇怪的是,有些数字performance得很尴尬,一天到了结尾,23:45,数字确实下降了,但是没有达到0的值,他们走了一点点,在logging的最后列表返回的值是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中的代码是我上面描述的尝试。

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

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).

还有更多的在线网站,您可以将date/时间转换为时间戳,反之亦然。 你可以从中进行validation。

对于解决scheme,您应该使用dataFormat来获取所需的格式。 一个例子就是在这里 。