Creating excel files through PHP
Mattias Geniar, Wednesday, June 4, 2008 - last modified: Sunday, February 19, 2012
No need to use complicated or extensive libraries (such as PEAR) to create Excel pages. Just take advantage of the "smart" part of Excel; its ability to parse an HTML table to a nice Excel page.
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):
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.
If that's not enough, you can look at more extensive libraries such as PHPExcel. As their examples show, it offers more advanced features.
Hi! My name is Mattias Geniar. I'm a Support Manager at Nucleus Hosting
in Belgium, a general web geek, public speaker
. If you're interested in keeping up with me, have a look at my podcast and weekly newsletter below. For more updates, follow me on Twitter as @mattiasgeniar
In the SysCast podcast I talk about Linux & open source projects, interview sysadmins or developers and discuss web-related technologies. A show by and for geeks!
A weekly newsletter - delivered every Sunday - for Linux sysadmins and open source users. It helps keeps you informed about open source projects, Linux guides & tutorials and the latest news.
Share this post
Did you like this post? Will you help me share it on social media? Thanks!