Friday, August 14, 2015

Poor man's way to generate an excel file in PHP

(Trick source)
Just create a regular .PHP file, where you output your data in a nice little html-table. Then place the following snippet in the top of that file (no output can happen before these lines, as they change your headers - so place these all the way at the top):

<?php
header(“Content-type: application/vnd.ms-excel”);
header(“Content-Disposition: attachment; filename=excel.xls”);

...

And it’s just that easy. If you open the page, you’ll see a download-window asking you where to place the file. The headers will tell your computer that it’s an Excel-file, which will parse your html-table (assuming you’ve written it without errors) and display it nicely.

No comments: