title: PHP Excel remove blank rows at top
body: <p>I am using PHP to export data from a grid view to an Excel spreadsheet.  For some reason, there are blank rows at the top.</p>
  
  <p><img src="https://i.stack.imgur.com/TfiFI.jpg" alt="enter image description here"></p>
  
  <p>As you can see in the image above, the headers begin printing at row 9, and of course the data starts to print afterward, that which I did not show.</p>
  
  <p>My code works and does what I need it to do.  I am just trying to figure out why it is printing blank rows at the top.</p>
  
  <p>Here is the code:</p>
  
  <pre><code> &lt;?php
     $ts = date('mdY-His');
     session_start();
     $where = $_SESSION['where'];  // this is the parameters passed from another file
  
     $sql = "SELECT * FROM mainTable WHERE " . $where . " ORDER BY CON_STATUS;";
     $result = mysql_query($sql) or die("&lt;script language='javascript'&gt;
                                         window.alert('There was no data to send')
                                         window.location.href='../home.php'
                                         &lt;/script&gt;");
     header("Content-Type: application/xls");
     header("Content-Disposition: attachment; filename=importdetails-".$ts.".xls");
     header("Pragma: no-cache");
     header("Expires: 0");
     header("Content-Transfer-Encoding: binary");
  
     $sep = "\t";
     for($i = 0; $i &lt; mysql_num_fields($result); $i++)
     {
       echo mysql_field_name($result, $i) . "\t";
     }
     print("\n");
     while($row = mysql_fetch_row($result))
     {
       $schema_insert = "";
       for($j = 0; $j &lt; mysql_num_fields($result); $j++)
       {
         if(!isset($row[$j]))
           $schema_insert .= "NULL".$sep;
         elseif ($row[$j] != "")
           $schema_insert .= "$row[$j]".$sep;
         else
           $schema_insert .= "".$sep;
       }
       $schema_insert = str_replace($sep."$", "", $schema_insert);
       $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
       $schema_insert .= "\t";
       print(trim($schema_insert));
       print "\n";
     }
   ?&gt;
  </code></pre>
  
  <p>As stated, my code works, besides the printing of the blank rows at the top of the spreadsheet.  If you see anything in my code that will remove the blank rows, that would be greatly appreciated.</p>