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.
To delete blank rows, use the Cells. deleteBlankRows() method.
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--;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With