Creating an Excel File from PHP

Okay, so this isn't really a Microsoft Excel file. I cheat and create a tab delimited file that, given the XLS file extension, Excel will happily open as if it were.

<?php
 
	// Trigger a download from the browser
	header("Content-Disposition: attachment; filename=\"foo.xls\"");
	header("Content-Type: application/vnd.ms-excel");
 
	// Echo the data, tab delimited
	echo "Item 1\tItem 2\tItem3\n";
	echo "9\t8\t7\n";
 
?>

As you can see, it's as simple as spitting out the appropriate header and then a bit of tab (/t) delimited data, one set of columns per line (/n).

comments powered by Disqus
php/creating_an_excel_file_from_php.txt · Last modified: 2020/06/01 22:53 (external edit)