How to loop through all the rows and cells in an excel file
Please consider using stream for a more declarative style iteration:
Workbook wb = WorkbookFactory.create(new FileInputStream("filename.xlsx"));
Sheet sheet = wb.getSheetAt(0);
StreamSupport.stream(sheet.spliterator(), false)
.filter(...)
.map(...)
.collect(Collectors.toList());
You can also use a common for-loop:
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
final Row row = sheet.getRow(i);
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
final Cell cell = row.getCell(j);
// do stuff to each cell here...
}
}
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