繁体   English   中英

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)将其转换为字符串 但是它仍然返回某种数字,我无法理解返回的值。

即它们返回如下:

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是我上面描述的尝试。

根据我的理解,返回值就是时间戳值。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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