2017-01-07 13 views
1

Javaを使用してExcelやExcelで繰り返し処理に問題があります。私はすべての数字を収集し、コンストラクタにそれを渡す必要がイテレータを前進させずにセル値を取得する方法 - Excel、Java POI

enter image description here

:私は、このようにシートに構造化されたデータを持っています。私はこのようにsthを試しました:

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

    } 

どのように対処するには?事前に 感謝:)

EDIT:

私は他の方法で細胞を取得することを決めた:

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) iであるため代わりにショー細胞値の問題を抱えていますイテレータの例のように、この値をコンストラクタに渡す必要があります。しかし、今私はループを使用しているセルへのアクセスを持っているので、次のセルの値を得ることができません.next()またはsthの類似 - 私は同じ行をコードの1行に次のセルを取得することはできません上記のように(イテレータで)、このコードを使用していますか?

+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

これは正常に動作するはずですが、それはdoesntです。なぜか分からない。このイテレーターのように見えますが、まだこの値を省略しています... – 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 + blank + cells): "CellIteratorはファイルに定義されているセルを返します。これは主に値やスタイルを持つものですが、Excelに依存します"。 –

0

Iteratorを使用することなく、他の方法 -

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)); 
関連する問題