簡體   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