应用if-else条件使用PhpExcel导出选择性记录

I need to export only those records whose column satisfies defined value. Eg.

if(null !== ($this->f3->get('SESSION.userStatus')))
    {
        $userStatus =  $this->f3->get('SESSION.userStatus');
    }

This is how I tried to set cell values depending on set value:

$rowID = 5;
foreach ($results as $result) 
   {
     if($result['status'] == $userStatus)
     {
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$rowID, $result['fullname']);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$rowID, $result['checkin_date']);
     }
   else
     {
        $objPHPExcel->getActiveSheet()->removeRow($rowID);
     }
     $rowID++;
   }

The file is exported with only those records that have defined "status" value. But the problem is that rows whose criteria doesn't meet are filled with spaces and still occupies rows. Here is how output looks like:

       A   |  B   |  C  |  D
  1        |      |     |
  .
  .
  174      |      |     |
  175      |      |     |
  176      |      |     |
  177      |      |     |
  178 John |  2014| xyz | dfdf
  179 Jack |  2015| jkl | dfdf
  180      |      |     |
  .
  .

How can I fix it to get records starting with top row? Eg.

1 | John   |   2014 |   xyz  | dfdf
2 | Jack   |   2015 |   jkl  | dfds

between } $rowID++; put this else { $objPHPExcel->getActiveSheet()->removeRow($rowID); }

That will delete your blank rows. Note, that you start with row 5 for some reason and do not test for max rows. Your code will still need some fixing up