Posted by stratosg in Quickies, Tutorials
on Mar 27th, 2008 | 0 comments
I’ve seen it happening to many sites and I always thought of this as impossible. I beleived that since the api is closed you cannot export into this format. It turns out that i was wrong. Well, as it turned out, not entirely… I mean the api is still closed but an XML can be parsed.
So all you have to do is write a file with the following format:
-
-
-
<workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
-
<worksheet ss:name="test.xls">
-
</worksheet></workbook>
-
<table><row>
-
<cell><data ss:type="String">foo</data></cell>
-
<cell><data ss:type="String">bar</data></cell>
-
</row>
-
<row>
-
<cell><data ss:type="String">bara</data></cell>
-
</row></table>
-
This is a perfect xls file that has two rows with two columns of data on the first and one on the second. Now, if you want to send this from a server to a browser use the following code:
-
header("Content-type: application/octet-stream");
-
header("Content-Disposition: attachment; filename=voters-export-".
date(‘d-m-Y’).
".xls");
-
header("Content-Type: application/ms-excel");
-
-
With this you are done!