我在使用java的Excel中對行和單元格進行迭代時遇到問題。我有片狀結構這樣的數據:如何在不移動迭代器的情況下獲取單元格值 - Excel,Java POI
我要收集所有的數字並把它傳遞給構造函數。我試過這樣:
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()或者類似的方法獲取下一個單元格的值 - 我無法在一行代碼中獲取下一個單元格,因此如何創建同一個對象如上(與迭代器),但使用此代碼?
「如何獲得單元格值而不向前移動」在繼續之前存儲該值? – Moira
@ 1blustone我不知道如何獲得這個值。閱讀我的解釋,不僅是一個標題:) – pulpet112
我做到了,這就是爲什麼我說要存儲它。迭代器繼續前進的問題是什麼?有一行像'Cell stored = cellIterator.next()',然後將它用於'precId [1]'?或者我沒有正確理解 – Moira