Java - Apache POI HSSF/XSSF - 无法读取数据格式 XX:XX 的单元格
Java - Apache POI HSSF/XSSF - Trouble reading cell with data format XX:XX
所以这就是问题所在,我正在尝试使用 Apace POI 库来读取包含以下格式的一些数据的 excel 文件:
Tag | Date | Hour | Value
X | 20150101 | 00:00 | 15
X | 20150101 | 00:15 | 16
X | 20150101 | 00:30 | 20
正如您想象的那样,Tag、Date 和 Value 列很容易获得。
我的问题是“小时”列,因为它以某种方式被归类为数值。我试图使用 cell.setCellType(CELL_TYPE_STRING); 将它变成一个字符串,但是它仍然是 return 某种我无法理解的数字的 returned 值。
即他们return如下:
00:00 --> 0
00:15 --> 1.04166666666666E-2
00:30 --> 2.08333333333332E-2
and so on...
奇怪的是,这些数字的行为很尴尬,一天结束后,在 23:45,数字确实下降了,但没有下降到 0 值,它们上升了一点, 在记录列表的末尾,值 returned 是
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 中的代码是我在上面描述的尝试。
据我了解,您得到的 return 值是时间戳值。
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).
还有很多可用的在线网站,您可以在其中将 date/time 转换为时间戳,反之亦然。你可以从中验证。
对于解决方案,您应该使用 dataFormat
来获得所需的格式。一个例子是 here.
所以这就是问题所在,我正在尝试使用 Apace POI 库来读取包含以下格式的一些数据的 excel 文件:
Tag | Date | Hour | Value
X | 20150101 | 00:00 | 15
X | 20150101 | 00:15 | 16
X | 20150101 | 00:30 | 20
正如您想象的那样,Tag、Date 和 Value 列很容易获得。 我的问题是“小时”列,因为它以某种方式被归类为数值。我试图使用 cell.setCellType(CELL_TYPE_STRING); 将它变成一个字符串,但是它仍然是 return 某种我无法理解的数字的 returned 值。
即他们return如下:
00:00 --> 0
00:15 --> 1.04166666666666E-2
00:30 --> 2.08333333333332E-2
and so on...
奇怪的是,这些数字的行为很尴尬,一天结束后,在 23:45,数字确实下降了,但没有下降到 0 值,它们上升了一点, 在记录列表的末尾,值 returned 是 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 中的代码是我在上面描述的尝试。
据我了解,您得到的 return 值是时间戳值。
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).
还有很多可用的在线网站,您可以在其中将 date/time 转换为时间戳,反之亦然。你可以从中验证。
对于解决方案,您应该使用 dataFormat
来获得所需的格式。一个例子是 here.