When to use rowiterator and iterator for iterating through the rows of an excel sheet

Viewed 10589

The code to read the contents of an excel spreadsheet (.xlsx) is included below. To iterate thru each row, I'm using iterator() method of sheet object, this works well. Also if I use rowIterator() method, it also works well.

What is the difference between these 2 functions and when to use which.

{

FileInputStream fis = new FileInputStream(new File("E:\\readexcel.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(0);
Iterator<Row> rowIterator = sh.iterator(); // sh.rowIterator(); -- also works well
while(rowIterator.hasNext()){               
    Row row = rowIterator.next();               
    Iterator<Cell> cellIterator = row.iterator(); //row.cellIterator();-- also works well               
    while(cellIterator.hasNext()){                  
        Cell cell = cellIterator.next();
        System.out.print(cell.getStringCellValue()+"\t");
    }
                System.out.println("");
}

}
1 Answers
Related