javaexceljxl

Read Excel file and skip empty rows but not empty columns


I wish to read an Excel file and skip the empty rows. My code skips empty cells but it skips empty columns, too. How can I skip empty rows but maintain empty columns? I'm using JXL Java.

for (int i = 0; i < sheet.getRows(); i++) {
    for (int j = 0; j < sheet.getColumns(); j++) {
        Cell cell = sheet.getCell(j, i);
        String con = cell.getContents();
        if (con != null && con.length() != 0) {          
            System.out.print(con);
            System.out.print("|");
        }
        else {
            continue;
        }
    }
}

Solution

  • Try this:

    for (int i = 0; i < sheet.getRows(); i++) {
        boolean rowEmpty = true;
        String currentRow = "";
        for (int j = 0; j < sheet.getColumns(); j++) {
            Cell cell = sheet.getCell(j, i);
            String con=cell.getContents();
            if(con !=null && con.length()!=0){
                rowEmpty = false;
            }
            currentRow += con + "|";
        }
        if(!rowEmpty) {
            System.out.println(currentRow);
        }
    }
    

    What you were doing is:

    What this does is: