0

How can I remove empty rows in an Excel document using Apache POI? The below code is throwing a NullPointerException when it encounters an empty row in the Excel file.

public class removeemptylines {

    public static void main(String args[]) throws IOException,
            InvalidFormatException {
        FileInputStream file = new FileInputStream(new File("v.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            XSSFRow row = sheet.getRow(i);
            if (row == null) {
                sheet.removeRow(row);
            } else {
                System.out.println("not empty");
            }
        }
    }
}

For example the source file might have rows:

viany
gdf
kumar

raj

fff

I would like the output to be:

viany
gdf
kumar
raj
fff
Bobulous
  • 12,967
  • 4
  • 37
  • 68
vinay
  • 25
  • 2
  • 8

0 Answers0