2016-08-24 45 views
1

我正在嘗試在Excel文件中查找特定日期的單元格引用。 Excel的設置使得「日期」標題在A1中,並且日期包含在B1,C1等中的同一行中。Java Apache POI - 創建迭代器以查找日期

我試圖創建一個迭代器來檢查這些日期並將其與輸入日期。 爲簡明起見,我省略了一些代碼。

public static void main(String[] args) throws IOException, ParseException { 
    SimpleDateFormat formatter ; 
     formatter = new SimpleDateFormat("MM/dd/yyyy"); 
     Date date = formatter.parse(args[0]); 

    // ............ 

    // iterator to find date 
    int dateRef = 0; //logs column of date 
    Date tempDate = null; 
    // while loop to check the date contents of each cell 
    while (dateRef < 1000) { // random number inserted 
     dateRef++; 
     tempDate = cell.getDateCellValue(); 
     if (date == tempDate) { 
      break; //beaks if contents are equal 
     } 
     cell = results.getRow(0).getCell(dateRef); // else continue and iterate one cell 
     } 
} 

當我運行這個時,迭代器繼續向上到列1000.我用打印語句檢查了這一點。即使單元格在正確的日期着陸,它也不會將輸入日期和單元格日期值視爲相同,因此不會中斷。

我知道這可能不是最有效的做事方式,所以如果你有更多的建議,讓我知道他們。謝謝!

回答