简体   繁体   English

Java-Apache POI HSSF / XSSF-数据格式为XX:XX的读取单元有问题

[英]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: 因此,这里的问题是,我试图使用Apace POI库读取具有以下格式的一些数据的excel文件:


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. 可以想象,Tag,Date和Value列很容易获得。 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); 我已经尝试通过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. 奇怪的是,这些数字表现得很尴尬,在一天结束时,在23:45时,数字确实下降了,但没有下降到0值,到记录结束时,它们有点上升了列出返回的值为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. 我知道缺少某些代码,尤其是在情况3中。情况2是我上面描述的尝试。

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. 对于解决方案,您应该使用dataFormat获得所需的格式。 One example is here . 这里是一个例子。

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

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