title: PHP Mysql table conversion to excel:The file you are trying to open is in a different format than specified by the extension
body: <p>I have a code that converts mysql table to excel sheet. It converts and the excel sheet is downloaded but whenever I try to open the file it gives an error msg in excel 2007:"The file you are trying to open is in a different format than specified by the extension...".The excel sheet opens after I click on 'Yes' but it is nothing but a white blank sheet.I have seen the similar posts but none of them solves my problem...Please help...Thank you</p>
  
  <pre><code>    &lt;?php
        include('dbcon.php');
      ?&gt;
  
      &lt;?php
  $sql = "SELECT * FROM stu_gen_info";
  $rec = mysql_query($sql) or die (mysql_error());
  
  $num_fields = mysql_num_fields($rec);
  
  for($i = 0; $i &lt; $num_fields; $i++ )
  {
      $header .= mysql_field_name($rec,$i)."\\t";
  }
  
  while($row = mysql_fetch_row($rec))
  {
      $line = '';
      foreach($row as $value)
      {                                            
          if((!isset($value)) || ($value == ""))
          {
              $value = "\\t";
          }
          else
          {
              $value = str_replace( '"' , '""' , $value );
              $value = '"' . $value . '"' . "\\t";
          }
          $line .= $value;
      }
      $data .= trim( $line ) . "\\n";
  }
  
  $data = str_replace("\\r" , "" , $data);
  
  if ($data == "")
  {
      $data = "\\n No Record Found!\n";                        
  }
  
  header("Content-Type: application/vnd.ms-excel");
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=reports.xls");
  header("Pragma: no-cache");
  header("Expires: 0");
  print "$header\\n$data";
  
      ?&gt;
  </code></pre>