Home arrow PHP arrow Page 3 - PHP and COM

The Number Game - PHP

You've already seen how PHP can be used to interface with Java components and JavaBeans. But here's something you didn't know - PHP can (shock shock! horror horror!) even be used to interface with Microsoft COM objects on the Windows platform. Will this be a happy marriage? Read on to find out.

TABLE OF CONTENTS:
  1. PHP and COM
  2. Striving To Excel
  3. The Number Game
  4. Export Potential
  5. Keeping It Simple
  6. Access-ing The Web
  7. All For One, And One For All
  8. New Coins For Old
  9. Link Zone
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 15
April 23, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
PHP's COM extension makes it easy to access and manipulate COM objects that have been registered on your system. One of these objects is the Excel.Application object, which exposes a number of methods and properties that can be creatively used by an imaginative developer.

The first step to using a COM object in your PHP script involves creating an instance of the COM class; to quote the PHP manual, this class provides you with "...a framework to integrate COM into your PHP scripts".

<?php // create an object instance $excel = new COM("Excel.Application") or die("Excel could not be started"); ?>
The argument passed to the class constructor is the name of the component to be used. In this case, since I want an instance of Microsoft Excel, I've used the Excel.Application object (more information on this object can be obtained at http://msdn.microsoft.com/library/en-us/modcore/html/deovrworkingwithmicrosoftexcelobjects.asp)

By default, PHP assumes that the component is available on the local server. In the event that you would like the component to be fetched from a remote DCOM server, the server name can be specified as a second, optional argument to the constructor. Note, however, that in order for this to work, the PHP configuration variable

com.allow_dcom = true
must be set, either in the PHP configuration file or via the ini_set() function call.

Once an object instance has been created, object methods and properties can be accessed using standard OO conventions.

<?php // pop open the Excel application $excel->Visible = 1; // turn off alerts $excel->DisplayAlerts = 0; // add a workbook $excel->Workbooks->Add(); // save $excel->Workbooks[1]->SaveAs("C:\\Inventory.xls"); // close the application $excel->Quit(); ?>
Since the Excel.Application object exposes a number of different methods and properties, you can use it to create workbooks, add sheets to the workbook, add or delete content to the cells of the worksheet, apply formulae to the cells, save the workbook to the disk...in fact, everything that you would be able to do with the application itself.

Keeping this in mind, the example above is fairly simplistic - all it does is create an Excel workbook and save it as a file - but it nevertheless demonstrates how easy it is to do something that, in theory at least, sounds very difficult ("hey Matt, any idea how to create an Excel spreadsheet in Windows using just PHP?").

 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: