title: How to Export from PHP to Excel (xlsx)
body: <p>I am trying to export to an xlsx file from a MSSQL query in PHP. I don't have access to the servers that my website is hosted on so i can't use anything that isn't already built into PHP. I'm using PHP 5.4. So far I've created a <code>&lt;a&gt;&lt;/a&gt;</code> that references a php page that should pull data from my MSSQL DB. Instead it gives me a blank file, in xls format. When I open it it throws an error about not having a stylesheet.css file in the downloads folder then opens a blank xls workbook. Here's my ExportToExcel.php file:</p>
  
  <pre><code>$tsql = "select ID, TableName, UpdateDate from pmdb.TableUpdates order by UpdateDate";
  //echo $tsql . "&lt;br&gt;";
  $getqueries = $conn-&gt;query($tsql);
  //echo "&lt;BR&gt;";
  //var_dump($getqueries);
  $result = $getqueries-&gt;fetchALL(PDO::FETCH_ASSOC);  
  $filename = "TableUpdates";
  //var_dump($tsql);
  
  header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  header("Content-Disposition: attachment; filename=$filename.xls");
  
  /*******Start of Formatting for Excel*******/
  
  //define separators (defines columns in excel &amp; tabs in word)
  $sep = "\t"; //tabbed character
  $br = "\r\n"; //line break
  
  //start of printing column names as names of SQL fields
  //$max = sqlsrv_num_fields($result);
  /* for ($i = 0; $i &lt; $max; $i++) {
  echo sqlsrv_field_metadata($result,$i) . "\t";
  }
  print("\n"); */
  
  foreach( sqlsrv_field_metadata( $result ) as $fieldMetadata ) {
  foreach( $fieldMetadata as $name =&gt; $value) {
     echo "$value" . $sep;
  }
  }   
  echo $br;
  
  //end of printing column names
  
  //start while loop to get data
  while($row = sqlsrv_fetch($result))
  {
      $schema_insert = "";
      for($j=0; $j&lt;sqlsrv_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";
  }
  </code></pre>
  
  <p>I got most of this from other questions, but none of them had an answer either.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have gotten a little further. I can now print the data to excel, though it still throws the errors </p>
  
  <blockquote>
    <p>The file format and extension of . . . don't match . . .</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>Problems During Load: Missing file: C:\Users\username\Downloads\StyleSheet.css</p>
  </blockquote>
  
  <p>,but everything prints in one column. It is comma separated, but all values are in the same column. Here's the new portion of the code:</p>
  
  <pre><code>//define separators (defines columns in excel &amp; tabs in word)
  $sep = ","; //tabbed character
  $br = "&lt;br&gt;"; //line break
  
  $hsql = "select Headings from TableHeadings where TableName = 'TableUpdates' order by ID";
  $getHeadings = $conn-&gt;query($hsql);
  $rHeadings = $getHeadings-&gt;fetchALL(PDO::FETCH_ASSOC);
  
  $headings = array($rHeadings[0]["Headings"],$rHeadings[1]["Headings"],$rHeadings[2]["Headings"]);
  
  //start of printing column names as names of SQL fields
  foreach($headings as $Heading =&gt; $value)
  {
      echo "$value" . $sep;
  }
  echo $br;
  for ($i = 0;$i &lt; 3;$i++)
  {
      for($l = 0;$l &lt; 3;$l++)
      {
          if ($l == 0)
          {
              echo $result[$i]["ID"] . $sep;
          }
          elseif ($l == 1)
          {
              echo $result[$i]["TableName"] . $sep;
          }
          else
          {
              echo $result[$i]["UpdateDate"];
          }
      }
      echo $br;
  }
  </code></pre>
  
  <p>How can I get this to echo each value and then move to the next cell? It does have commas, but looks like this:</p>
  
  <pre><code>ID,TableName,UpdateDate,
  1,pmdb.MaterialTracking,2016-07-08 13:45:05.963
  2,pmdb.OSP_OPR_Report,2016-07-08 13:45:45.883
  3,pmdb.COEI_OPR_Report,2016-07-08 13:45:47.920
  </code></pre>