title: PHPExcel generating real xls and xlsx files from database
body: <p>I tried PHPExcel download package test 01. The example used object to specify which cell contains what, and produce an excel file.</p>
  
  <p>Besides, I tried the following codes to generate an excel file from database:-</p>
  
  <pre><code>    $file_type = "vnd.ms-excel";
      $file_ending = "xls";
  
  
  //header info for browser: determines file type ('.doc' or '.xls')
  header("Content-Type: application/$file_type");
  header("Content-Disposition: attachment; filename=$file_name.$file_ending");
  header("Pragma: no-cache");
  header("Expires: 0");
  
  $xls_header = '&lt;html xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns="http://www.w3.org/TR/REC-html40"&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-type" content="text/html;charset=utf-8" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table border="1" align="center"&gt;';
  
  $xls_footer = '&lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;';
  print $xls_header;
  
  /*  Start of Formatting for  Excel  */
  
  
      /*  FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
      //create title with timestamp:
      if ($Use_Title == 1)
      {
          echo "&lt;tr&gt;&lt;td colspan='".mssql_num_fields($result)."'&gt;";
          echo("$title");
          echo "&lt;/td&gt;&lt;/tr&gt;";
      }
      //define separator (defines columns in excel &amp; tabs in word)
      //$sep = "\t"; //tabbed character
  
          echo "&lt;tr&gt;";
      //start of printing column names as names of MySQL fields
      for ($i = 0; $i &lt; mssql_num_fields($result); $i++)
      {
          echo "&lt;td&gt;";
          echo mssql_field_name($result,$i);
          echo "&lt;/td&gt;";
      }
      print("&lt;/tr&gt;");
      //end of printing column names
  
      //start while loop to get data
      while($row = mssql_fetch_array($result))
      {
          $schema_insert .= "&lt;tr&gt;";
          //set_time_limit(60); // HaRa
          $schema_insert = "";
          for($j=0; $j&lt; mssql_num_fields($result);$j++)
          {
              $schema_insert .= "&lt;td&gt;";
              if(!isset($row[$j]))
                  $schema_insert .= "NULL".$sep;
              elseif ($row[$j] != "")
                  $schema_insert .= "$row[$j]".$sep;
              else
                  $schema_insert .= "".$sep;
              $schema_insert .= "&lt;/td&gt;";
          }
          $schema_insert .= "&lt;tr&gt;";
          $schema_insert = str_replace($sep."$", "", $schema_insert);
          //following fix suggested by Josue (thanks, Josue!)
          //this corrects output in excel when table fields contain \n or \r
          //these two characters are now replaced with a space
          $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
          $schema_insert .= "\t";
          print(trim($schema_insert));
          print "\n";
      }
      print $xls_footer;
  </code></pre>
  
  <p>but excel program informs me that it is not a real excel file. if I wish to generate real excel files for xls and xlsx format with data from database, is there any good example?</p>
  
  <p>Thanks!</p>