Have the HSSFSheet and HSSFRow implement the Iterator interface - this would allow the Java 5 foreach to be used resulting in cleaner code. Considering there is only one Iterator method on each class, it is clear what the iterator() class would return.
HSSFRow.cellIterator and HSSFSheet.rowIterator should already provide what you want. Use row.cellIterator or sheet.rowIterator in your foreach loop
Sadly, Nick Burch is mistaken. If you test the code sample "Iterate over rows and cells (including Java 5 foreach loops)" at http://poi.apache.org/hssf/quick- guide.html#Iterator, you will find that it generates a compiler error "Can only iterate over an array or an instance of java.lang.Iterable". (Was this tested before publishing?) For this to work, Noel Markham's original suggestion should be implemented. For HSSFSheet and HSSFRow to be Iterable, there must be a method called iterator(). Alternatively (and perhaps more logically), you could return Iterable data structures through HSSFSheet.rows() and HSSFRow.cells().
Additionally, the Iterator returned by iterator() should be typed - i. e. return an Iterator<HSSFRow> or Iterator<HSSFSheet>. This avoids compiler warnings about type safety and increases user confidence.
(In reply to comment #3) > Additionally, the Iterator returned by iterator() should be typed - i. e. > return an Iterator<HSSFRow> or Iterator<HSSFSheet>. This avoids compiler > warnings about type safety and increases user confidence. Typing requires Java 1.5, and POI is aiming to stay working with Java 1.4, so that isn't going to be possible
It seems that the way sun introduced foreach loops in java 1.5 was rather stupid, and prevents your code from then working on java 1.4. Since we require java 1.4 compatibility, we can't implement java.lang.Iterable (though trunk does have the method that that requires). For now, you'll need to just use the normal iterator methods In the ooxml branch, we do have some workarounds for java 1.4 vs java 1.5 features, so Sheet/HSSFSheet/XSSFSheet and Row/HSSFRow/XSSFRow do implement java.lang.Iterable (when build on java 1.5), so foreach loops will work there.