Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete blank rows in between the sheet using POI?

I want to delete blank row from Excel sheet using POI.

We have method like shiftRow() or removeRow() but its not for use in this case. Please help me to get it done.

like image 827
Shahnwaz Alam Avatar asked Sep 12 '12 06:09

Shahnwaz Alam


People also ask

How do you delete blank rows in Excel in Java?

To delete blank rows, use the Cells. deleteBlankRows() method.


1 Answers

Please try following piece of code. It works for me as well when more than one consecutive blank rows existing.

    for(int i = 0; i < sheet.getLastRowNum(); i++){
        if(sheet.getRow(i)==null){
            isRowEmpty=true;
            sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
            i--;
        continue;
        }
        for(int j =0; j<sheet.getRow(i).getLastCellNum();j++){
            if(sheet.getRow(i).getCell(j).toString().trim().equals("")){
                isRowEmpty=true;
            }else {
                isRowEmpty=false;
                break;
            }
        }
        if(isRowEmpty==true){
            sheet.shiftRows(i + 1, sheet.getLastRowNum(), -1);
            i--;
        }
    }
like image 130
Sankumarsingh Avatar answered Sep 21 '22 04:09

Sankumarsingh