Home > PHP > Creating excel files through PHP

Creating excel files through PHP

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-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.

Spread the word
If you liked the content of this article, please vote for it on the following websites - thanks.
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • StumbleUpon
  • Sphinn
  • Facebook
  • Live
  • Mixx
  • Technorati
  • TwitThis

Matti PHP , ,

  1. Joshua
    June 26th, 2008 at 20:29 | #1

    Nice method very simple and quick, highly recommended

  2. GwB
    July 3rd, 2008 at 00:36 | #2

    You just saved me a lot of time. Very elegant…

  3. July 11th, 2008 at 06:26 | #3

    It seems to work in IE, but doesn’t work in Safari or Firefox. Is there something else I need for those browsers?

    Thanks for the great tip. Certainly a lot easier then other solutions I’ve found.

  4. July 11th, 2008 at 06:56 | #4

    Never mind. It worked beautifully once I entered the filename correctly on the second header line.

    Thanks again for such a huge simple piece of code.

  5. July 11th, 2008 at 08:11 | #5

    Hi Keith, thanks for the comment. It is indeed one of the easier solutions, but it only works when browsing directly to a page, and downloading the file.

    I have yet to find a very simple method for exporting it on a regular basis (through cronjobs), without having user interaction.

    If anyone has any idea on how to do so, please let me know :-)

  6. Tejas
    August 11th, 2008 at 12:17 | #6

    really nice method..
    but i have one question…
    can I create multiple excel files using single Header() in single PHP code..???
    if possible, please reply me. I need it very urgent.
    Thank You!..

  7. August 11th, 2008 at 13:00 | #7

    Hi Tejas,
    You can only create one file at a time using this method, allthough you could open multiple windows which call this page multiple times. A main page with some iFrames that load your PHP Excel page?

    This method really is only useful for a quick export of your data, not for multiple files that you want to store on your hard disk. It’ll be better to use dedicated classes for that.

  8. January 22nd, 2009 at 04:55 | #8

    Very nice trick.

    Thx a lot ;)

  9. February 3rd, 2009 at 07:14 | #9

    Great Trick man!!
    Awesome!

  10. -
    February 27th, 2009 at 10:58 | #10

    It works and I’ve used it with some simple data. Problem with this system is that you can’t control how excel interprets your data. You will end up with numbers converted to dates and some other problems.

    There are some free components for doing this so you’re safer using those.

  11. April 10th, 2009 at 03:09 | #11

    Clean, elegant, efficient and most important: it does the job!

    Great post!

  12. May 25th, 2009 at 11:13 | #12

    Does this trick work with background colors of tables cells and rows? Thx

  13. May 25th, 2009 at 12:34 | #13

    @Chris: it should, as your Excel application will just parse the HTML table and convert it to Excel rows/columns.

  14. July 20th, 2009 at 21:15 | #14

    Simply awesome. I nested some CSS and had a brilliant looking Excel file generated from PHP.

  15. August 4th, 2009 at 08:49 | #15

    nice and helpful article. I tried so meny method to this and this is the best thing but all Excel sheets give an error in opening.(Asks whether it is from a trusted source.) but Grideline are not visible. Any idea about making these guidelines visible?
    thank you.

  16. James
    September 21st, 2009 at 13:18 | #16

    I tried this for word 2007 and it throws an error that you have to click through in order to get to the excel page. Anyway around this?

  17. September 21st, 2009 at 22:11 | #17

    I doubt it, but haven’t tried it yet. It’s a work-around for simple Excel exports, but it’s flawed in many ways since it will have to parse HTML code, where it was expecting properly formatted Excel code. You might be able to trick it by passing some headers, to simulate Excel 2003?

  18. him_aeng
    September 26th, 2009 at 06:52 | #18

    Simply the BEST!!!!!

    Thank you for your rescue. YOU SAVE MY LIFE.

  19. Emmanuel
    November 4th, 2009 at 12:35 | #19

    Thank you so much for the simple but nice and efficient tutorial. How ever, am having a question. How can I set the generated Excel to be in read & write mode?

  20. maddog
    November 26th, 2009 at 10:35 | #20

    How save thats files to zip? Is it posible ?

  21. November 26th, 2009 at 10:40 | #21

    @Maddog; you could use PHP’s ZIP functionality, to process the Excel output and run it through a compression filter; http://be.php.net/zip. Not the recommended way to do this, though.

  22. Rick
    December 15th, 2009 at 14:58 | #22

    Thanks for this, didn’t know I could just use html tables this helped me a lot.

  23. Dragan
    January 9th, 2010 at 19:51 | #23

    I am having trouble exporting some latin characters i.e PHP code works great but OpenOffice failes to show all the characters.. .any help on this? Thanks !

  24. Bernat
    January 18th, 2010 at 11:12 | #24

    Using this way, is it possible to create an excel file with multiple sheets? Thnx.

  25. ndtwc
    January 19th, 2010 at 06:21 | #25

    It works perfectly! Thanks!

  1. No trackbacks yet.