Wednesday, May 27, 2009

Quick-tip: Simple Excel export from PHP

There is a very very simple way to export Excel-files without any
special libraries or classes.

The cheat, and it is a cheat but who cares when Excel doesn't :), is
to export a html table and call it an excel file. That's it. Simple,
right?

• Point to an URL with an "xls" file extension (use parseExtensions is
a good idea). "example.com/stats/weekly.xls"

• Set the output headers. RequestHandler wants:
$this->RequestHandler->setContent('xls', 'application/vnd.ms-
excel');

• Output a table in your view. Do not output a html page. Just the
table will do. Please add some 90's formatting to the table to get
some borders. Add any h# tags and p tags before and after the table to
add some information and labeling.


I did not learn this until recently when I was sent an excel-file that
my excel import could not read. At first I thought it was some new
xlsx variation that my mac could not read but it turned out it was a
html table masked as an excel file.

I thought this might be useful to some of you. It is a lot less
painful than most excel libraries when you just want to export some
stats page as excel. Your view will probably already have a table of
data.

/Martin


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: