CakePHP - Export data to Excel - The easy way!

26
Oct
48

I am posting this method here just so we never ever ever ever…
forget what Albert Einstein once said

Everything should be made as simple as possible, but not simpler.

Exporting data to Excel files from a database can be done in so many different ways, using so many libraries and classes such as PEAR’s Spreadsheet_Excel_Writer

But here comes the easiest and simplest alternative if you are too busy to read too much instructions, or can’t install any modules or libraries to PHP on your host.

Large code snippets are great, but to save you some copying and pasting just download the source.

In the sample code, my model is called “Myview”, although not included in the zip as it can be any model you want, nothing goes in there.
You will also find the controller, layouts and views related.

All you need to is call that export_xls() method in myview_controller.php

All it does is:
- fetch your data with find(’all’)
- Renders the layout called “export_xls.ctp” in view/layouts
- Renders the view called “export_xls.ctp” in view/myview

REMEMBER:
- Excel document grid is a simple html table
- You can stylize and format your table using CSS

That’s it!

Found this useful?

Why not subscribe to my RSS feed!

Filed under: CakePHP

// 48 Comments

  1. sufian yusuff
    3:17 am on November 12th, 2009

    dude… its a brilliant art of programming… nice job man

  2. doode968
    5:14 am on November 24th, 2009

    Thanks for such an clear and simple method. I second the brilliant comment above.

  3. Frederick D.
    12:14 am on November 26th, 2009

    This was AMAZING! I have literally spent a whole month trying to make the export work using another method. The quote from Albert Einstein is among my most favorite expressions. I was also able to make this solution work coming from an ExtJS grid.

    Now if I can conquer filtering with CakePHP and ExtJS, my project will be complete.

    Thank you again!

  4. Josh
    2:22 am on December 8th, 2009

    This is great, but what about exporting 17000+ records? This results in a 17 MB file, which takes forever to load, but goes down to 3.5 MB when re-saving in Excel.

  5. Bachir El Khoury
    2:06 am on December 13th, 2009

    Frederick: Glad it helped. I must admit I spent as much time as you thinking there must be a simpler way :)

    Josh: yeah that can be troublesome when exporting large scale data. The reason why re-saving from Excel results in a much smaller file size, is that Excel converted text to binary. Try to open the new file in a text editor you will notice what i mean.

    If you really need to export large set of data, then it would be worth investing the time and setting up the Pear’s spreadsheet excel writer I mentioned.

  6. Brett
    10:46 pm on January 5th, 2010

    This seems to be the solution I’ve been looking for. I’ve installed everything as instructed but nothing happens. In Firefox, I’m redirected to /views/home.ctp for some reason and in Safari nothing happens at all. It’s definitely calling the function, but I’m never prompted to save the file.

    Sorry so vague….any ideas?

  7. Bachir El Khoury
    10:21 am on January 6th, 2010

    hey Brett, not sure why that is happening, as long as the model exists and your are rendering the proper view with the proper layout it should work as expected.

    If you can zip up the sample code you are working with and send it to info at dnamique dot com, I will be able to help you out.

  8. jgutix
    3:42 pm on February 2nd, 2010

    Excellent, thank you very much

  9. nphp101
    10:31 am on February 9th, 2010

    Indeed, simply the best!

  10. Gert
    12:04 pm on February 16th, 2010

    Thanks; works great!

    One thing though: Excel gives the following error “the file you are trying to open ‘report.xls’ is in a different format than specified by the file extension.” ; when I open it it works perfectly, but still doesn’t look too good.

    Any suggestions? Thanks.

  11. Hanno
    10:53 am on February 19th, 2010

    Great idea! Never thougt it would be so easy!

  12. Bachir El Khoury
    3:47 pm on February 19th, 2010

    Gert: yeah I had that problem as well in latest versions of Excel. It doesn’t prevent the doc from opening though like you mentioned.
    As in regards to how it looks, you can use CSS to style it if you want.

  13. Dave
    11:01 pm on February 25th, 2010

    Thanks, mate, good work

  14. Marcus Lenngren
    2:37 pm on March 3rd, 2010

    Very nice and good post!

  15. eL.
    9:38 am on March 13th, 2010

    brilliant implementation of KISS methodology.

  16. blurp
    6:54 am on March 25th, 2010

    works like a charm
    hahaha, this is actually fooling the browser by set the header as xls format
    nice one!!

  17. Heidy
    8:31 am on April 2nd, 2010

    Thanks.
    I adapted this idea to the symfony framework and it works

  18. Mauricio
    8:02 am on April 13th, 2010

    Hi, my developer plataform is Linux so I open the xls file with openoffice, but it tried to open as a csv file, after checking my code several times, I open the file in a ms windows computer and It works! So there is a way to make this openoffice compatible?Thanks in advance!

  19. Mark D.
    7:28 pm on June 11th, 2010

    In IE8, this works perfectly on my development server but not on my live server. The only difference is that the live server uses SSL. Any suggestions?

  20. Kevin Lambert
    2:31 am on June 17th, 2010

    Thanks a lot for sharing this. I was wondering if there is a way to make the output compatible with MS Excel 2010. It would give a warning with 2007 about the format being different from the specified extension but it would totally fail with 2010. Any possible hints?

  21. Javier Murillo
    8:05 pm on September 14th, 2010

    Love it!! thank you very much !!!

  22. Martin Atukunda
    4:01 pm on October 7th, 2010

    The zip file contains a view/ folder instead of a views/ folder.

  23. david sinha
    7:07 pm on October 21st, 2010

    its a great one !!!!!

    THANKS BUDDY…………..!!!!!!!

  24. juan carlos
    12:12 am on November 4th, 2010

    men, muchas gracias, funciona perfecto!! :)

  25. Md. Shaiful Islam
    8:29 pm on December 10th, 2010

    Thanks for this simplesome script :)

  26. Thulasi
    6:23 am on December 24th, 2010

    Hi,

    Its great and simple working fine

    thanks a lot

  27. Euronymous
    7:45 pm on January 13th, 2011

    Hi…Thanks for the script…it’s great and simple…But I have a question: If I want to put a macro (I’ve already write it) where or how i do that?

    I hope you can help me and forget my poor English…

    Thanks a lot!!!

  28. Josh
    3:17 pm on February 1st, 2011

    Nice, this gave me good input. Thanks! :-)

  29. Bachir El Khoury
    2:12 am on February 2nd, 2011

    @Euronymous: an Excel macro you mean?
    Forget about it, you can’t do that as far as I know.
    This method here is just a simple way for a quick excel file output, nothing more.

  30. Tapan Kumar Thapa
    8:20 am on February 2nd, 2011

    Hi,

    Thanks for this beautiful piece of code.

    It works.

    Regards
    Tapan Thapa

  31. John Maxim
    5:58 pm on February 8th, 2011

    Hello, I just read a comment above that says Microsoft Excel 2010 couldn’t get it opening ? Can someone confirm with this ?
    Thanks for this idea and codes. :-)

    Thanks a lot for sharing this. I was wondering if there is a way to make the output compatible with MS Excel 2010. It would give a warning with 2007 about the format being different from the specified extension but it would totally fail with 2010. Any possible hints?

  32. Mukesh
    5:43 pm on February 10th, 2011

    Hi,
    This is working excellent at out local server, but when migrate code to live server then it create problem. Instead of Exporting Excel file it is showing the data in html format. Would be really thankful if provide any help.

    Thanks in advance..

  33. Muhaimin
    3:57 am on March 4th, 2011

    This solution solved my problem,

    It works like a charm!!

    TQVM

  34. Bachir El Khoury
    12:48 am on March 9th, 2011

    @John: This is has to do with MIME type setup on the other server. Check this link for more info, on how you can force the download of the file. http://apptools.com/phptools/force-download.php

  35. james
    2:17 pm on May 17th, 2011

    great job that is very good thanks

  36. Jean
    12:24 pm on May 22nd, 2011

    Thanks for that, very helpfull.
    But if I try to manually add a SUM formula in the gererated file, for any column, it won’t work, and will sum up to 0. Any ideas how to fix that ?

  37. Bachir El Khoury
    7:45 am on May 23rd, 2011

    @Jean
    This method here is just a simple way for a quick excel file output, nothing more. You won’t be able to add any functions nor macros, they simply won’t work.

  38. Diego Costa
    6:05 pm on June 1st, 2011

    Thanks you so much for sharing!
    It couldn’t be any easier than this…
    Now I just have to adjust it to my own needs.
    Keep up with the good work! :)

  39. sloga
    4:52 pm on July 28th, 2011

    Am I missing something? I don’t get prompted to download an excel file. I’m developing in Cake 1.1 and use file extension .thtml. I am able to view the export_xls page in my browser with the correct model data but not being prompted to download a “Report.xls” file. Any ideas? Thanks.

  40. kim
    1:34 pm on September 15th, 2011

    THANK YOU! In 10 minutes I had the report I needed. You sir, are a hero!

  41. Helio Ricardo
    1:17 pm on October 28th, 2011

    Fantastic, but don’t work for Google Docs and Numbers (Mac).

    I’m trying to fix up!

    Thank you,
    Helio

  42. Sujoy
    6:18 am on December 14th, 2011

    That s great…thank you.

  43. Ghaly
    11:54 pm on January 2nd, 2012

    i try it it’s so good, thank you very much
    but i have a problem with it which is when i export arabic data from my database
    it display as unknown letters when i open excel file

    so can you give me a solution for this problem ..
    thank you again :)

  44. ersaky
    3:58 pm on January 12th, 2012

    That’s a good,simple and useful solution.
    Thanks so much..

  45. Bachir El Khoury
    9:16 pm on January 16th, 2012

    @Ghaly glad it helped but I am sorry I cannot help you with the language issue. Checkout Stackoverflow for some answers, this issue is more of a character encoding Issue not specific to this script.

  46. Bachir El Khoury
    9:19 pm on January 16th, 2012

    If you care sharing your solution I can update the post. Thx

  47. Bachir El Khoury
    9:40 pm on January 16th, 2012

    @sloga this is another issue not related to this script. Check this link for a solution http://drupal.org/node/417866

  48. Bachir El Khoury
    9:40 pm on January 16th, 2012

    @sloga this is another issue not related to this script. Check this link for a solution http://drupal.org/node/417866

Leave a comment

RSS feed for comments on this post