How to check is excel file is blank?

Below is the code to check the worksheet is empty (blank worksheet)

class ExcelEmptyCheck{

Public static void main(String[] args) throws FileNotFoundException, IOException {
Workbook wBook = new XSSFWorkbook(new FileInputStream(“D:\\devicexls\\test_xls.xlsx”));
for (int i = 0; i < wBook.getNumberOfSheets(); i++) {
System.out.println("Sheet :: " + wBook.getSheetName(i) + " has data:: " + isSheetEmpty(wBook.getSheetAt(i)));
}
}

static boolean isSheetEmpty(Sheet sheet) {
Iterator rows = sheet.rowIterator();
Row row = null;
Cell cell = null;
while (rows.hasNext()) {
row = rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
cell = cells.next();
if (!cell.getStringCellValue().isEmpty()) {
return true;
}
}
}
return false;
}
}

Leave a comment