title: How to remove empty rows between row data in excel using POI HSSF Library in Java
body: <p>I need help to remove empty row between created list of records. i already code to remove empty rows however it able removed the first empty row only. here is the code i done:</p>
  
  <pre><code>private static void writeExcelFile(String[] keyValue, String fileName, String contents,
          ArrayList&lt;String&gt; listProperties, ArrayList&lt;String&gt; listPropertiesDescription,
          ArrayList&lt;String&gt; listPropertiesFileName) {
  
      int rownum = 2;
      HSSFSheet firstSheet;
      HSSFWorkbook workbook = null;
  
      workbook = new HSSFWorkbook();
      firstSheet = workbook.createSheet("Resourcebundle");
      Row headerRow = firstSheet.createRow((short) 1);
      headerRow.setHeightInPoints(30);
  
      headerRow.createCell((short) 0).setCellValue("Properties Name");
      headerRow.createCell((short) 1).setCellValue("Properties Description");
      headerRow.createCell((short) 2).setCellValue("Properties File Name");
  
      System.out.println("listPropertiesDescription ::  " + listPropertiesDescription.size());
      System.out.println("listPropertiesFileName ::  " + listPropertiesFileName.size());
      System.out.println("listProperties all list ::  " + listProperties.toString());
      System.out.println("listPropertiesDescription all list ::  "
              + listPropertiesDescription.toString());
  
      int indexProperties = 0;
      for (int i = rownum; i &lt; listProperties.size(); i++) {
  
          //            Row row = firstSheet.getRow(i + 1);
          Row row = firstSheet.getRow(i);
          //            System.out.println("row ::  " + row);
  
          if (row == null) {
              //                row = firstSheet.createRow(i + 1);
              row = firstSheet.createRow(i);
  
          }
  
          System.out.println("check index :: " + indexProperties);
  
          for (int j = 0; j &lt; 1; j++) {
              Cell cell = row.getCell(j);
              System.out.println("cell ::  " + cell);
  
              if (cell == null) {
                  row.createCell(j).setCellValue(
                          listProperties.get(indexProperties + 1).toString().trim());
                  row.createCell(j + 1).setCellValue(
                          listPropertiesDescription.get(indexProperties + 1).toString().trim());
                  row.createCell(j + 2).setCellValue(
                          listPropertiesFileName.get(indexProperties + 1).toString().trim());
              }
              j++;
          }
          indexProperties++;
  
          System.out.println("check index below ::  " + indexProperties);
  
          i++;
  
      }
  
      int lastRowCount = firstSheet.getLastRowNum();
  
      for (int i = rownum; i &lt; lastRowCount; i++) {
          HSSFRow row = firstSheet.getRow(i);
  
          if (row == null) {
              removeRow(firstSheet, i);
              //                firstSheet.shiftRows(i + 1, lastRowCount, -1);
              //                i--; // Since you move row at i+1 to i 
          }
      }
  
      FileOutputStream fos = null;
      try {
          File file = new File("OnlineHelp Master Excel.xls");
  
          //if file doesnt exists, then create it
          if (!file.exists()) {
              file.createNewFile();
          }
  
          String fileRB = outputLocation.concat("\\" + file);
          fos = new FileOutputStream(new File(fileRB));
          workbook.write(fos);
          fos.close();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  
  public static void removeRow(HSSFSheet sheet, int rowIndex) {
      int lastRowNum = sheet.getLastRowNum();
      if (rowIndex &gt;= 0 &amp;&amp; rowIndex &lt; lastRowNum) {
          sheet.shiftRows(rowIndex + 1, lastRowNum, -1);
      }
      if (rowIndex == lastRowNum) {
          HSSFRow removingRow = sheet.getRow(rowIndex);
          if (removingRow != null) {
              sheet.removeRow(removingRow);
          }
      }
  }
  </code></pre>
  
  <p>here i attach also result from the above code:</p>
  
  <p><img src="https://i.stack.imgur.com/MTstb.jpg" alt="enter image description here"></p>
  
  <p>the results:</p>
  
  <pre><code>Row | Result
  1   | result a
  2   | (empty row)
  3   | result b
  4   | (empty row)
  5   | result b
  6   | (empty row)
  </code></pre>
  
  <p>it only able to removed empty row below result a, the rest still there.</p>
  
  <p>really need help on this. thanks!</p>
  
  <p>ema</p>