A blend of programming and seo

How to create Microsoft Office Documents with PHP

There are two main ways to build Excel, Word, and PowerPoint documents using PHP. The first is by using the COM library (only if you are using a Windows server) and the other is by using a more standardized approach such as HTML or CSV.


Dynamically creating a word document:


<?php
$word = new COM('word.application');

$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';

//Setup the font
$word->Selection->Font->Name = 'Verdana';
$word->Selection->Font->Size = 8;

//Write some text
$word->Selection->TypeText('This is a test document');
//Save the document as DOC file
$word->Documents[1]->SaveAs('c:\\docs\\test1.doc');

//quit and release COM resources
$word->quit();
$word->Release();
$word = null;

?>

Dynamically creating an excel document

<?php
$excel = new COM('excel.application');
$excel->Visible = 0;

//Create a new workbook
$wkb = $excel->Workbooks->Add();
$sheet = $wkb->Worksheets(1);

//This code adds the text 'myvalue' on row 2, column 4
$sheet->activate;
$cell = $sheet->Cells(2,4);
$cell->Activate;
$cell->value = 'myvalue';

$wkb->SaveAs('C:\docs\test.xls');

//close and free resources
$wkb->Close(false);
$excel->Workbooks->Close();
$excel->Quit();
?>

Dynamically creating a powerpoint presentation

<?php
$powerpnt = new COM('powerpoint.application');
//Creating a new presentation
$pres=$powerpnt->Presentations->Add();
//Adds the first slide. '12' means blank slide
$pres->Slides->Add(1,12);
//Adds another slide. '10' means a slide with a clipart and text
$pres->Slides->Add(2,10);
//Adds a textbox
$pres->Slides[1]->Shapes->AddTextbox(1,20,50,300,40);
//Save the document as PPT file
$powerpnt->Presentations[1]->SaveAs('C:\Docs\test1.ppt');

//free resources and quit powerpoint
$powerpnt->quit();
?>

How to find Word, Excel, and Powerpoint functions

The following will show you all of the functions that are possible when accessing Microsoft Office components through php:

officephp How to create Microsoft Office Documents with PHP

  1. Open Microsoft Word, Excel, or Powerpoint
  2. Press Alt+F11 to start the Visual Basic Editor
  3. Press F2
  4. Find “ThisDocument” on the left. In the right frame you’ll see the available variables and functions that can be used with the COM object.
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Reddit
  • Twitter
  • HackerNews
  • StumbleUpon
  • Technorati

5 comments

1 ajay singh rathore { 08.12.09 at 4:44 am }

Please show some ajax example for msoffice.
I think there should be some code in php and ajax

2 Bob { 02.17.10 at 4:24 pm }

What bad tutorial… At least say beforehand that you will NOT be showing how to create one using HTML instead of calling it “How to create Microsoft Office Documents with PHP”. Call this tutorial: “How to create Word, Excel and Powerpoint documents using COM libraries.”

3 Roy (rawseo) { 02.17.10 at 4:29 pm }

@bob:

I’m sorry if you thought I was showing you how to create an office document with HTML.

Office documents are all binary formats. HTML is an ascii format. I don’t really think there is any way to create an office document with just an HTML page. I called it this because when you use the code provided, it will generate an office document with PHP.

4 Eduardo { 03.07.10 at 12:33 pm }

You can generate a Word document using a free PHP (LGPL) library: phpdocx.com

5 Barnabas { 03.15.10 at 9:41 am }

how do you open, read and extract doc file text content to display on the web or text-area input file

Leave a Comment