COM functions in PHP4 (Windows version) _php Tutorial

Source: Internet
Author: User
Tags ole adobe distiller
COM functions in PHP4 (Windows version) these days have been writing Excel into MySQL, found an article, search the PHPX forum, without this post, put it reprint as follows:


COM functions in the PHP4 (Windows version)


Introduced

COM functions built into the PHP4 are quite attractive for us to develop programs in the Win32 environment, but there is still not much relevant technical documentation. This article will be divided into three examples

Don't work with MS office Word, Excel, Adobe Distiller to explain how to use COM functions in PHP.

COM technology was proposed and developed by Microsoft a few years ago, the relevant nouns mentioned in this article have OLE, Ole Automation, ActiveX, COM, the meaning of these words are basically one

A package of code (objects) to accomplish some of the features of a Windows application. The PHP4 COM function can connect an object instance and use its method with the

Property.

If you want to use the following example source code, please refer to my configuration.

Windows 98-ms Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI


COM tags in the PHP4

Now let's start by instantiating a component with PHP4 COM, which requires the "OLE program identity" of the new operator and the object:


$instance = new COM ("$identifier");

?>

Because COM is a PHP4 reserved word, it transmits this object's identity to a constructor, now gets an instance of this component, according to the nature of the OOP class, we can easily

To access its methods and properties.

For example:


$instance->[object]->[method1]->[method2]-> ->[property];

?>

It's so simple!

The structure of OOP does not work under PHP (due to the PHP syntax problem, the name of the property. Values are illegal characters, such as dots and parentheses, etc.), so PHP4 provides two corresponding functions:


BOOL Com_set (class Com_object, string property name, string property_value);

Mixed Com_get (class Com_object, string property_name);

?>

Finally, PHP4 also supports DCOM technology, which allows you to create an object instance on a remote computer.


$Instance = new COM (string "Component name", string "remote_server_address");

?>

Note: This is using the DCOM directive to set PHP. In the future, PHP developers will provide support for DCOM under UNIX.

Identities, methods, and properties

The identity is a string of the following:

MS Word: "Word.Application" or "Word.application.9"
MS Excel: "Excel.Application" or "Excel.Sheet"
ADOBE Acrobat: "Exch.application" or "Pdfdistiller.pdfdistiller"

For the last identity, I would like to point out that getting the correct object ID name is not an easy task. If you can't access the VBA documentation, you can look for Windows registration

Table, looking in HKEY_CLASSES_ROOT, you can get some application names. The valid object ID on your machine is placed under the CLSID subfolder.

An application typically provides a document that describes its COM methods and properties. In office2000, you can run the program, open the VBA editor, and select Object Editor. Input application

A method name or property name in the library, and then, in the window below, select a class or member name with the right mouse button, and you will get a description of the class or member as you click Help. You also

You can refer to MSDN. An example of Excel is as follows: [Url]http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm[/url]


Using COM functions to manipulate MS Word

Now, let's start with the first example:


#*********************************************************
# This example comes from Zend site, slightly changed
# Open a Word instance and create a new document useless Test.doc
# Enter a line of text "This is a test2 ..."
#*********************************************************

#实例化一个对象

$word = new COM ("Word.Application") or Die ("Unable to instantiate word");

#取得并显示版本

Print "Loaded Word, Version {$word->version}
";

#另一种方法去取得版本

$testversion = Com_get ($word->application,version);

Print "Version using Com_get (): $testversion
";

#使其可见

$word->visible = 1;

#创建新文件

$word->documents->add ();

#写字符

$word->selection->typetext ("This is a test ...");

#保存

$word->documents[1]->saveas ("Useless test.doc");

#关闭

$word->quit ();

?>

Just take a few minutes to read the program and refer to the OLE technical documentation in Word, and you'll learn almost everything you need to do in your own program.

MS excel in COM functions that use PHP

As in the example above, you should learn this example while referencing the help documentation for the Object Browser in Excel's Visual Basic Editor.


#打开workbook和它的sheet,
#本例使用一个电子表格是Excel安装时自带的SOLVSAMP. XLS

$workbook = "C:Program filesmicrosoft Officeofficesamplessolvsamp.xls";
$sheet = "Quick Tour";

#实例化一个组件的对象
$ex = new COM ("Excel.Sheet") or Die ("Do not Connect");

#取程序名称和版本
Print "Application name:{$ex->application->value}
" ;
Print "Loaded version: {$ex->application->version}
";

#打开工作本使我们可使用它
$WKB = $ex->application->workbooks->open ($workbook) or Die ("didn't not Open");

#预保存原来的工作本, create a copy of the work book
$ex->application->activeworkbook->saveas ("Ourtest");
# $ex->application->visible = 1; #本句去注释让Excel可见

# Read and write a cell in a new worksheet
# We can read this 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}
"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
Print "New value = {$cell->value}
"; #Print the new value=15000

#最后, recalculate the cell with the new value
$sheets->calculate;
#必须的如果要计算, manual is optional
#可看到效果总价值 (E13 cell)
$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.
";
#根据计算公式, advertising affects the company's expenses, which will show $57,809

#使用Excel内建的函数
# PMT (PERCENT/12 months,number of Payments,loan amount)
$pay = $ex->APPLICATION->PMT (0.08/12,10,10000);
$pay = sprintf ("%.2f", $pay);
Print "Monthly payment for $ loan @8% INTEREST/10 months: $ $pay
";

#Should Print Monthly payment = $ -1,037.03

#可选, save
$ex->application->activeworkbook->saveas ("Ourtest");
#关闭, no questions.
$ex->application->activeworkbook->close ("False");
Unset ($EX);

?>

This example lets your PHP work with Excel, and of course, there are more objects to use, and accessing a self-written OOP wrapper class is as easy as accessing Excel.

Using PHP com to access Adobe Distiller

This last example is not MS program, if your program has a PostScript file, you will be interested in this, rewriting (distilling) it into a PDF document. Adobe has a

A program called Distiller, which can generate an instance. The code is as follows:


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

?>

One thing to note is that the OLE identity name "Pdfdistiller" given in distiller's documentation is not valid.

The most basic way to distill a file is to:


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

?>

This $psfile is the PostScript file name, Stroutputpdf is the file name of the output file PDF. Strjoboptions is the parameter file name of distiller, the last two parameters

is optional, the default is the same name. This PS filename is associated with the PDF file name, using this default Joboptions file. For example:


$pdf->filetopdf ($psfile, "", "");
#这儿 $psfile can be myfile.ps will return the Myfile.pdf file.

?>

More methods and properties are used in distiller. If you are interested, please refer to Adobe's technical documentation.


Abort/possible problems

If something is wrong in your code, you may have created an instance, but did not gracefully close it. Worst of all, this application may be maintained by this instance,

, there are multiple copies of the program in your program list, even if you correct the error will interfere with your results. The workaround: Fix a bug to clear them in time

Before you start again, use and end the task. For the same reason, at the end of your code, you should close the program and delete the instance in time.

You have some tricks when dealing with com_get and com_set anomalies. For example:
$Version = Com_get ($instance->application, "Version");

Will work in Word but will produce an error in Excel.

Some objects cannot be instantiated in PHP4, because this program wants a custom interface, but PHP4 does not support it.


Why should we use it?

I hope that these three examples can give you some clues to think, PHP's COM allows you to access the WINDOWS4 program in PHP scripts. This code is simpler than ASP and can integrate with other

PHP's powerful support for the database. Microsoft is aggressively selling this COM technology in every way, under different names and structures, such as COM + (Combine com with

Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, and more. The combination of PHP and Apache provides an open source solution.

http://www.bkjia.com/PHPjc/314786.html www.bkjia.com true http://www.bkjia.com/PHPjc/314786.html techarticle com functions in PHP4 (Windows version) these days have been writing Excel into MySQL, found an article, search the PHPX forum, no this post, put it reprint as follows: PHP4 (Windows version) ...

  • 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.