CakePHP - Export data to Excel - The easy way!
Oct13
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!

