Lines 324-345
Link Here
|
324 |
</section> |
324 |
</section> |
325 |
<anchor id="Iterator"/> |
325 |
<anchor id="Iterator"/> |
326 |
<section><title>Iterate over rows and cells</title> |
326 |
<section><title>Iterate over rows and cells</title> |
327 |
<p>Sometimes, you'd like to just iterate over all the rows in |
327 |
<p>Sometimes, you'd like to just iterate over all the sheets in |
328 |
a sheet, or all the cells in a row. This is possible with |
328 |
a workbook, all the rows in a sheet, or all the cells in a row. |
329 |
a simple for loop.</p> |
329 |
This is possible with a simple for loop.</p> |
330 |
<p>Luckily, this is very easy. Row defines a |
330 |
<p>These iterators are available by calling <em>workbook.sheetIterator()</em>, |
331 |
<em>CellIterator</em> inner class to handle iterating over |
331 |
<em>sheet.rowIterator()</em>, and <em>row.cellIterator()</em>, or |
332 |
the cells (get one with a call to <em>row.cellIterator()</em>), |
332 |
implicitly using a for-each loop. |
333 |
and Sheet provides a <em>rowIterator()</em> method to |
333 |
Note that a rowIterator and cellIterator iterate over rows or |
334 |
give an iterator over all the rows. These implement the |
334 |
cells that have been created, skipping empty rows and cells.</p> |
335 |
<em>java.lang.Iterable</em> interface to allow foreach loops.</p> |
|
|
336 |
|
335 |
|
337 |
<source> |
336 |
<source> |
338 |
Sheet sheet = wb.getSheetAt(0); |
337 |
for (Sheet sheet : wb ) { |
339 |
for (Row row : sheet) { |
338 |
for (Row row : sheet) { |
340 |
for (Cell cell : row) { |
339 |
for (Cell cell : row) { |
341 |
// Do something here |
340 |
// Do something here |
342 |
} |
341 |
} |
|
|
342 |
} |
343 |
} |
343 |
} |
344 |
</source> |
344 |
</source> |
345 |
</section> |
345 |
</section> |