Actually, this is much easier than you might think. The trick is to use the capability of excel and word to read html files
So just generate normal html, but use a content-type header line to force the browser to see this as a word or excel file: (using php)
header (“Content-type: application/msword”); or
header (“Content-type: application/vnd.ms-word”);
for word and
header (“Content-type: application/msexcel”); or
header (“Content-type: application/vnd.ms-excel”);
I have never seen any difference between msword and vnd.ms-word, so if anybody knows the difference, please respond.
Then you might want to define the local filename. This can be done using the content-disposition header:
header(“Content-Disposition: filename=\”invoice_$invoicenr.xls\””);
For excel, just start generating a html table. The full table is used as excel sheet with its rows and columns.
For word, just start generating html code. It is interpreted by word. Make sure to use a FULL url path if you want to include images.
Here is an example to generate a word document:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<? $inr=10056; // invoice number $item="usbst"; // article number $itemdesc="Usb stick 512Mb"; // article descr $price=6.49; // price header("Content-type: application/vnd.ms-word"); header("Content-Disposition: filename=\"invoice_$inr.doc\""); Header("Expires: Wed, 14 Oct 1997 06:41:40 GMT"); Header("Cache-control: no-cache"); ?> <html> <HEAD> <style type="text/css"> body { font-family: "Microsoft Sans Serif"; font-size:10pt; margin:0px;} img { padding:0px;margin:0px;border: solid 0px white;} </style> </HEAD><BODY> <img src="http://www.cosninix.com/blog49/themes/default/images/header750x100.jpg" width="100"> <div style="margin-left:12px;"> <p>Invoice #<?=$inr?></p> <table width="100%"> <tr> <td width="20%"><b><?=$item?></b></td> <td width="40%"><?=$itemdesc?></td> <td width="40%"><?=$price?></td> </tr> </table> </body> </html> |
Click here to test it.