I want to use foreach to iterate through all the cells in my excel file in order to set a single foreground color. This is what I have so far.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet = wb.getSheetAt(0);
for (HSSFRow myrow : sheet){
for (HSSFCell mycell : myrow){
//set foreground color here
}
}
The problem is for the statements for (HSSFRow myrow : sheet)
and for (HSSFCell mycell : myrow)
I am getting:
Can only iterate over an array or an instance of
java.lang.Iterable
I checked HSSFSheet
and HSSFRow
- they implement java.lang.Iterable(Row)
and java.lang.Iterable(Cell)
respectively.
You can also do this:
Try this. It compiles ok
I am using POI 3.7 Stable
Please consider using stream for a more declarative style iteration: