Posts Tagged ‘spreadsheet’

Create Excel file from PHP with tables using headers

Did You ever needed to create pure and simple Excel spreadsheets from php? Are you tired with complicated classes, large and nonsense code? Here is a tiny code sample, showing how easy it  is to create Excel files directly from a php file. I won’t comment the lines, as the code is obvious…

$file_name = “myfile.xls”;

header(‘Pragma: public’);
header(‘Expires: 0′);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(‘Cache-Control: public’);
header(‘Content-Description: File Transfer’);
header(‘Content-Type: text/xls; name=”‘ . $file_name . ‘”‘);
header(‘Content-Disposition: attachment; filename=”‘ . $file_name . ‘”‘);
header(‘Content-Transfer-Encoding: binary’);

$content = ‘<table>
<tr>
<td>Header1 (A)</td>
<td>Header2 (B)</td>
<td>Header3 (C)</td>
</tr>
<tr>
<td>Content 1A</td>
<td>Content 1B</td>
<td>Content 1C</td>
</tr>
<tr>
<td>Content 2A</td>
<td>&nbsp;</td>
<td>Content 2C</td>
</tr>
</table>’;

echo $content