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.

25 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!!:).

  16. kishore 10 March 2012 at 06:17 #

    thanx for image full path suggestion

  17. Casey Dux 13 March 2012 at 19:54 #

    I genuinely enjoy examining on this internet site , it has wonderful content . “The longing to produce great inspirations didn’t produce anything but more longing.” by Sophie Kerr.

  18. Samantha Steck 19 March 2012 at 17:52 #

    I believe what you posted made a bunch of sense. But, consider this, suppose you added a little content? I ain’t suggesting your information isn’t good, but suppose you added a headline to possibly get people’s attention? I mean Generating microsoft office documents (word, excel) on your webserver using php | Cosninix is a little vanilla. You could look at Yahoo’s front page and watch how they create post headlines to get viewers to click. You might try adding a video or a related pic or two to get people excited about what you’ve written. Just my opinion, it could make your blog a little livelier.

  19. Felton Jermeland 26 March 2012 at 12:03 #

    You could certainly see your skills in the paintings you write. The world hopes for more passionate writers such as you who are not afraid to say how they believe. All the time go after your heart. “If you feel yourself falling, let go and glide.” by Steffen Francisco.

  20. Geniere 27 March 2012 at 06:37 #

    I hope you never ever put a stop to! This can be among the top blogs Ive ever before read. Youve got some mad talent right here, man. I just hope you dont shed your type given that youre without a doubt one of many coolest bloggers to choose from. Be sure to preserve it up as the internet demands a person such as you spreading the word.

  21. Stacy Keibler Interview 2 April 2012 at 14:20 #

    I believe this is among the so much important information for me. And i am happy studying your article. However should remark on some general issues, The site taste is wonderful, the articles is truly great : D. Good activity, cheers

  22. 27 inch computer monitor reviews 7 April 2012 at 18:06 #

    I feel 1 of your advertisements triggered my browser to resize, you could would like to place that in your blacklist.

  23. Plymouth letting agent 11 April 2012 at 23:52 #

    Spot on as per usual. Yet another good article!

  24. cheap jeans for men 12 April 2012 at 13:08 #

    hello net master, your weblog pagea€?s themes is striking mixed with supporting executing it. Any composes seem to be amazing. Make certain to maintain pace the very good give superior benefits. Meets.

  25. Cierra Skoog 27 April 2012 at 22:35 #

    It’s appropriate time to make a few plans for the longer term and it is time to be happy. I have read this submit and if I may I wish to recommend you few fascinating things or tips. Perhaps you can write subsequent articles referring to this article. I desire to read more things approximately it!

Leave a Reply