user582485 user582485 - 1 month ago 7
Java Question

POI: using the foreach to iterate through all the cells in an excel file

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. any suggestions?

Answer

Try this. It compiles ok

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
sheet = wb.getSheetAt(0);

for (Row myrow : sheet) {
    for (Cell mycell : myrow) {
        //set foreground color here
    }
}

I am using POI 3.7 Stable