I needed to return an Excel file from a PHP script I wrote.  The simplest way to do this is to create a page consisting of a single table and then setting the headers correctly.  Excel will read the HTML table and convert it automatically into an excel spreadsheet – much easier than writing out a raw Excel file!

To do this, generate your HTML for the table and place it in a variable (don’t print it):

$html = "
";
$html .= "
";
$html .= "


";

Then print out the headers as follows:

header('Content-type: application/msexcel');
header('Content-disposition: attachment; filename=export.xls');
print $html;

And the result will be a excel file that the browser will download and open in MS Excel!

Leave a Comment

Leave a Reply

My column My column 2
January 2008
M T W T F S S
    Feb »
 123456
78910111213
14151617181920
21222324252627
28293031