From d8f457d03ed13128a6c919387c7d2e419e2f0cc6 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 30 Mar 2008 14:03:37 +0000 Subject: [PATCH] Various quickguide updates git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@642740 13f79535-47bb-0310-9956-ffa450edef68 --- .../content/xdocs/spreadsheet/quick-guide.xml | 32 +++++++++---------- .../ss/usermodel/examples/FromQuickGuide.java | 14 ++++++-- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml index 528582beb..76f3263fe 100644 --- a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml +++ b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml @@ -254,21 +254,20 @@

Sometimes, you'd like to just iterate over all the rows in a sheet, or all the cells in a row. This is possible with a simple for loop.

-

Luckily, this is very easy. HSSFRow defines a +

Luckily, this is very easy. Row defines a CellIterator inner class to handle iterating over the cells (get one with a call to row.cellIterator()), - and HSSFSheet provides a rowIterator() method to + and Sheet provides a rowIterator() method to give an iterator over all the rows.

-

(Unfortunately, due to the broken and - backwards-incompatible way that Java 5 foreach loops were - implemented, it isn't possible to use them on a codebase - that supports Java 1.4, as POI does)

+

Alternately, Sheet and Row both implement java.lang.Iterable, + so if you're using Java 1.5, you can simply take advantage + of the built in "foreach" support - see below.

- HSSFSheet sheet = wb.getSheetAt(0); + Sheet sheet = wb.getSheetAt(0); for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) { - HSSFRow row = (HSSFRow)rit.next(); + Row row = (Row)rit.next(); for (Iterator cit = row.cellIterator(); cit.hasNext(); ) { - HSSFCell cell = (HSSFCell)cit.next(); + Cell cell = (Cell)cit.next(); // Do something here } } @@ -284,26 +283,25 @@ } -
Iterate over rows and cells using Java 1.5 foreach loops - OOXML Branch Only +
Iterate over rows and cells using Java 1.5 foreach loops

Sometimes, you'd like to just iterate over all the rows in a sheet, or all the cells in a row. If you are using Java 5 or later, then this is especially handy, as it'll allow the new foreach loop support to work.

-

Luckily, this is very easy. Both HSSFSheet and HSSFRow +

Luckily, this is very easy. Both Sheet and Row implement java.lang.Iterable to allow foreach - loops. For HSSFRow this allows access to the + loops. For Row this allows access to the CellIterator inner class to handle iterating over - the cells, and for HSSFSheet gives the + the cells, and for Sheet gives the rowIterator() to iterator over all the rows.

- HSSFSheet sheet = wb.getSheetAt(0); - for (HSSFRow row : sheet.rowIterator()) { - for (HSSFCell cell : row.cellIterator()) { + Sheet sheet = wb.getSheetAt(0); + for (Row row : sheet) { + for (Cell cell : row) { // Do something here } } - This only works on the OOXML branch of POI
Text Extraction diff --git a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java index 88433e1b5..8563413d8 100644 --- a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java +++ b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java @@ -24,8 +24,6 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CreationHelper; -import org.apache.poi.ss.usermodel.DataFormat; -import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -125,4 +123,16 @@ public class FromQuickGuide { wb.write(fileOut); fileOut.close(); } + + public void iterating() { + Workbook wb = new HSSFWorkbook(); + Sheet sheet = wb.createSheet("new sheet"); + + for (Row row : sheet) { + for (Cell cell : row) { + // Do something here + System.out.println(cell.getCellType()); + } + } + } }