COM functions in PHP4 (Windows)--there are examples in which Word documents read and write. (Part II)

Source: Internet
Author: User
Tags end functions interface ole php and php script adobe distiller
Window|word Using the PHP4 COM functions with MS Excel
As for the Word example above, study the code and the help from the Visual Basic Editor objectbrowser for Excel.
<?php

#Set the workbook to use and its sheet. In the example we use a spreadsheet that
#comes with the Excel installation Called:solvsamp. Xls

$workbook = "C:\Program Files\Microsoft Office\office\samples\solvsamp. XLS ";
$sheet = "Quick Tour";

#Instantiate the Spreadsheet component.
$ex = new COM ("Excel.Sheet") or Die ("Did not Connect");

#Get the application name and version
Print "Application name:{$ex->application->value}<br>";
Print "Loaded version: {$ex->application->version}<br>";

#Open the workbook we want to use.
$WKB = $ex->application->workbooks->open ($workbook) or Die ("Did not Open");

#Create a copy of the workbook, so the original workbook would be preserved.
$ex->application->activeworkbook->saveas ("Ourtest");
# $ex->application->visible = 1; #Uncomment to make Excel visible.

# Read and write to a cell in the new sheet
# We want to read the cell E11 (advertising in the 4th. Quarter)
$sheets = $wkb->worksheets ($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->cells (11,5); #Select the cell (Row Column number)
$cell->activate; #Activate the cell
Print "Old Value = {$cell->value} <BR>"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
Print "New value = {$cell->value}<br>"; #Print the new value=15000

#Eventually, recalculate the sheet with the new value.
$sheets->calculate; #Necessary only if Calc. option is manual
#And the effect on Total cost (Cell E13)
$cell = $sheets->cells (13,5); #Select the cell (Row Column number)
$number = Number_format ($cell->value);
Print "New Total cost =\$ $number-was \$47,732 before.<br>";
#Should Print $57,809 Because the advertising affects the Corporate overhead in the
# cell formula.

#Example of the built-in functions in Excel:
#Function: PMT (percent/12 months,number of Payments,loan amount)
$pay = $ex-&GT;APPLICATION-&GT;PMT (0.08/12,10,10000);
$pay = sprintf ("%.2f", $pay);
Print "Monthly payment for $10,000 loan @8% INTEREST/10 months: \$ $pay <BR>";
#Should Print Monthly payment = $ -1,037.03

#Optionally, Save the modified workbook
$ex->application->activeworkbook->saveas ("Ourtest");
#Close all workbooks without questioning
$ex->application->activeworkbook->close ("False");
Unset ($EX);

?>
This is example should get your going with the Excel COM and PHP. Of course there are many more objects to use. Writing an OOP wrapper for the principal functions'll make access to the Excel objects even.
Using PHP COM with Adobe Distiller
This is the last example are for a non-ms program:if your program has produced a PostScript document and it may be interesting to TR Ansform it (distill it) to a PDF document. Adobe has a program called Distiller with a Windows version so can instantiated, with the following code:
<?php

$pdf = new COM ("Pdfdistiller.pdfdistiller.1");

?>
Note This OLE Identifier name is isn't obvious, especially when the distiller documentation (ADOBE ' s technical note #515 8) refers to it as "Pdfdistiller."
The principal method to distill a document is:
<?php

$pdf->filetopdf ($psfile, Stroutputpdf ', strjoboptions ");

?>
Where $psfile is the name of the PostScript file, Stroutputpdf are the name for the output PDF file. Strjoboptions is the name of the parameters file for Distiller. The two last parameters of the ' method ' can be-left blank to-use the ' same name, the PS file for the ' PDF file and to ' Default Job options file. For example:
<?php

$pdf->filetopdf ($psfile, "", "");
#Where $psfile could is myfile.ps and the result file:Myfile.pdf

?>
There are more methods and properties that can is used with Distiller. If you are are interested, look at the Adobe's technical note.
caveats/possible problems
If There are some errors in your code, your may instantiate the object and your the ' not ' close before it times out. Worst of all, the application may retentively is instantiated. As a result, several copies could lay around in your programs list and interfere after you have the corrected. The Solution:after fixing the bug, clean up (<CTRL+ALT+Delete> and end Task) all of the instances in the program list Before you restart. For this same reason, always close the application on the end of your code and unlink the instance.
You could experience some oddities with Com_get and Com_set. For example: $Version = Com_get ($instance->application, "Version"); Works with Word, but produces a error with Excel.
Some Objects won ' t is instantiated by PHP4, it appears this Objects need a custom interface that php4-com doesn ' t su Pport.
Why use it?
Hopefully, these are three examples have shown you the ropes. PHP COM allows the connection to many Windows programs inside a PHP script. The code is simpler than ASP ' s and can being integrated with the rest of the PHP ' s powerful database functions. Microsoft markets the "COM technology everywhere and under different names and architectures, like COM + (Combine com with Mi Crosoft Transaction Server MTS, ADO, OLE DB, OWC, Windows DNA, etc. PHP and Apache, working together, are now offering a open source solution to this confusion.
Http://phpclasses.upperdesign.com/browse.html?package=86
--alain
Ps:see the EXCEL class using the "COM interface" at:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.