2017-01-07 10 views
1

我在使用java的Excel中對行和單元格進行迭代時遇到問題。我有片狀結構這樣的數據:如何在不移動迭代器的情況下獲取單元格值 - Excel,Java POI

enter image description here

我要收集所有的數字並把它傳遞給構造函數。我試過這樣:

Iterator<Row> iterator = firstSheet.iterator(); 
while (iterator.hasNext()){ 
     Row nextRow = iterator.next(); 
     Iterator<Cell> cellIterator = nextRow.cellIterator(); 
     int id = (int) cellIterator.next().getNumericCellValue(); 
     int pr = (int) cellIterator.next().getNumericCellValue(); 
     int[] prId = new int[2]; 

     prId[0] = (int) cellIterator.next().getNumericCellValue(); 

     iterator.next(); // i have to move down one row to get 5 and 6 

     precId[1] = // what should I type here?? cellIterator.next() will cause, that cell with value "5" will be omitted and I will get "6" 
     int co = (int) cellIterator.next().getNumericCellValue(); 
     list.add(new Rule(id, pr, prId, co)); 

    } 

如何處理? 感謝提前:)

編輯:

我決定讓其他方式細胞:

for(int rowNum = rowStart+1; rowNum < rowEnd; rowNum++){ 
     Row r = sheet.getRow(rowNum); 
     if(r == null){ 
      continue; 
     } 
     int lastColumn = Math.max(r.getLastCellNum(), 4); 
     for(int cn = 0; cn < lastColumn; cn++){ 
      @SuppressWarnings("deprecation") 
      Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL); 
      if(c == null){ 
       continue; 
      }else{ 
       System.out.println(c); 
      } 
     } 
    } 

該解決方案顯然更好,但我通過System.out.println(c)有因,而不是表演細胞值的問題,我需要像迭代器那樣將這個值傳遞給構造函數。但是現在我使用循環訪問單元格,所以我不能使用.next()或者類似的方法獲取下一個單元格的值 - 我無法在一行代碼中獲取下一個單元格,因此如何創建同一個對象如上(與迭代器),但使用此代碼?

+0

「如何獲得單元格值而不向前移動」在繼續之前存儲該值? – Moira

+0

@ 1blustone我不知道如何獲得這個值。閱讀我的解釋,不僅是一個標題:) – pulpet112

+0

我做到了,這就是爲什麼我說要存儲它。迭代器繼續前進的問題是什麼?有一行像'Cell stored = cellIterator.next()',然後將它用於'precId [1]'?或者我沒有正確理解 – Moira

回答

0

初始化nextRow變量和cellIterator再次 -

Iterator<Row> iterator = firstSheet.iterator(); 
while (iterator.hasNext()){ 
     Row nextRow = iterator.next(); 
     Iterator<Cell> cellIterator = nextRow.cellIterator(); 
     int id = (int) cellIterator.next().getNumericCellValue(); 
     int pr = (int) cellIterator.next().getNumericCellValue(); 
     int[] prId = new int[2]; 

     prId[0] = (int) cellIterator.next().getNumericCellValue(); 

     nextRow = iterator.next(); // i have to move down one row to get 5 and 6 
     cellIterator = nextRow.cellIterator(); 
     cellIterator.next(); // for first empty cell 
     cellIterator.next(); // for second empty cell 

     precId[1] = (int) cellIterator.next().getNumericCellValue(); 
     int co = (int) cellIterator.next().getNumericCellValue(); 
     list.add(new Rule(id, pr, prId, co)); 

    } 

請注意,在循環中再次調用iterator.next()可能會導致一些RuntimeException的,它始終是更好地撥打iterator.hasNext()首先,檢查返回值,然後調用iterator.next()

+0

這應該工作正常,但它does not。我不知道爲什麼。看起來這個迭代器仍然省略了這個值... – pulpet112

+0

終於有效了!我不知道爲什麼,但'cellIterator.next();'應該只調用一次 – pulpet112

+1

@Vikas Sachdeva:這段代碼不是很好的做法。閱讀[迭代單元格,控制缺失/空白單元格](https://poi.apache.org/spreadsheet/quick-guide.html#Iterate+over+cells%2C+with+control+of+missing+%2F +空格+單元格):「CellIterator將只返回文件中定義的單元格,這主要是那些帶有值或樣式的單元格,但它依賴於Excel」。 –

0

不使用迭代器的其他方式 -

for (int rowNum = rowStart + 1; rowNum < rowEnd; rowNum = rowNum + 2) { 

     Row oddRow = sheet.getRow(rowNum); 
     if (oddRow == null) { 
      continue; 
     } 
     int[] prId = new int[2]; 
     int id = (int) oddRow.getCell(0).getNumericCellValue(); 
     int pr = (int) oddRow.getCell(1).getNumericCellValue(); 
     prId[0] = (int) oddRow.getCell(2).getNumericCellValue(); 

     Row evenRow = sheet.getRow(rowNum); 
     if (evenRow == null) { 
      continue; 
     } 
     prId[1] = (int) evenRow.getCell(2).getNumericCellValue(); 
     int co = (int) evenRow.getCell(3).getNumericCellValue(); 
     list.add(new Rule(id, pr, prId, co)); 
相關問題