Generating microsoft office documents (word, excel) on your webserver using php

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:

<?
$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.

15 Responses to “Generating microsoft office documents (word, excel) on your webserver using php”

  1. mini cooper s for sale 4 February 2011 at 05:55 #

    would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

  2. iTunes Gift Card 14 February 2011 at 18:49 #

    Nice blog post. Keep blogging. Please visit mine when you have the time. Thanks

  3. infertility treatment 19 February 2011 at 00:02 #

    Hi there! I simply want to give a huge thumbs up for the good info you could have right here on this post. I will probably be coming back to your weblog for extra soon.

  4. Christopher Alcosiba 5 March 2011 at 02:28 #

    I can see that you are are genuinely passionate about this! I am trying to build my own website and you’ve helped me with some great information.

  5. Zilvinas Juraska 10 March 2011 at 06:19 #

    My coder is trying to convince me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a year and am worried about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be really appreciated!

  6. backlink checker 8 April 2011 at 08:39 #

    [...] Cosninix » Generating microsoft office documents (word, excel) on your webserver using php [...]

  7. dymatize 10 April 2011 at 21:29 #

    may you go in to higher detail with this specific article? i would like to learn much more about it, mainly the second paragraph

  8. 1st birthday party supplies 16 April 2011 at 05:38 #

    [...] Cosninix » Generating microsoft office documents (word, excel) on your webserver using php [...]

  9. mini boxer 22 April 2011 at 05:19 #

    [...] Cosninix » Generating microsoft office documents (word, excel) on your webserver using php [...]

  10. Travel and Entertainment 15 May 2011 at 06:30 #

    Sir, I think your article give refrence for preparation of my site. Waiting new blogpost related with niche. TY.

  11. Legal sounds 8 June 2011 at 11:43 #

    We are a group of volunteers and starting a new initiative in our neighborhood. Your site provided us with valuable information to help us get started|.You have done a great job!

  12. Manufacturer Coupons 14 June 2011 at 01:20 #

    Thanks for the informative article. My wife’s calling me for dinner So I need to run off without reading as much as I’d like. But I put your blog on my RSS feed so that I can read more.

  13. Find A Property 19 June 2011 at 04:55 #

    Good info. I reached on your website by accident, so thanks.

  14. Auricolari Bluetooth Samsung 30 June 2011 at 23:13 #

    We stumbled over here by a different web address and thought I might check things out. I like what I see so now i’m following you. Look forward to looking into your web page yet again.

  15. dieta usp 1 February 2012 at 08:29 #

    I would like to find a weblog the place i will just type any situation that pertains to intellect. Kind of like your log but is not for the reason that personal. Good quality sites when I can get started our web log 100 % free and additionally where by most people notice???? Satisfy along with considerably thank you preferred!!:).

Leave a Reply