• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

COM Functions in PHP4 (Windows)--里面有例子来讲WORD文档读写的。

发布: 2007-7-01 15:06 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网 COM Functions in PHP4 (Windows)
Alain M. Samoun
Introduction
The built-in COM functionality of PHP4 is quite attractive for some of us programming in the win32 environment. So far, there is not much documentation on the subject. This short article will explain how to use COM in real PHP4 programming with three examples using MS office 2000 Word and Excel programs and the Adobe Distiller program. The COM technology has been developed by Microsoft for several years, under different names. As far as this article is concerned, the words OLE, OLE Automation, ActiveX and COM are all the same: They designate an encapsulated piece of code (the Object) that performs some functions for a windows application. PHP4 COM connects to the object (Instantiate the object) and uses its methods and properties.
If you want to reproduce the following examples, here is my configuration:
Windows 98 - MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI
COM tags in PHP4
Lets start with the specific information to use the COM functions with PHP4. To instantiate a component, one needs the "new" operator and the "OLE programmatic identifiers" of the object:
<?php

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

?>
Since COM is a reserved class name in PHP4, it passes the object@#s identifier to the constructor. Now that we have instantiated the component, we can easily reach its methods and properties, using the OOP class. For example:
<?php

$instance->[Object]->[method1]->[method2]->..->[property];

?>
It@#s that simple!
There are two tag functions for PHP4 COM that are used when the OOP construct doesn@#t work. (In the case of PHP syntax problems, with the names and values of properties with invalid characters, like dot or parenthesis):
<?php

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 to create an instance of an object on a remote computer:
<?php

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

?>
Note: there is a DCOM directive to set in the PHP configuration. PHP developers may add DCOM support for Unix in the future. That@#s all, there are no other functions to remember!
Identifiers, methods and properties.
identifiers are strings like:
    For MS Word:   "Word.Application" or "Word.Application.9"
    MS Excel:      "Excel.Application" or "Excel.Sheet"
    ADOBE Acrobat: "Exch.application" or "PdfDistiller.PdfDistiller"
As the last identifier name indicates, it is not always easy to know the right name for the object. If you do not have access to a VBA doc, you can look at the windows registry (Start - Run regedit) and look in the HKEY_CLASSES_ROOT folder: Scan down until the end of the extensions list, you will then reach the Application names. The COM Identifiers available in your machine, are the folders with the CLSID subfolders.
The application program should document its COM methods and properties. In the case of Office 2000, start the application, then open the visual basic editor with the <ALT+F11> short cut key and select the Objects Editor <F2>. Enter a name of method or properties for the application@#s library. Then, select a class or member and right click on the member name in the next window below. You will get the description for the class or member by selecting help. You can also consult MSDN. An example for Excel is: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm
Using PHP4 COM functions with MS Word
Now, we have all we need to start with the first code example:
<?php

#*********************************************************
# This example, slightly modified from the Zend site,
#  will open an instance of word with a new
# document with the name "Useless test.doc" and the line:
# "This is a test2..." typed inside.
#*********************************************************

#Instantiate the Word component.

$word = new COM("word.application") or die("Unable to instantiate Word");

#Get and print its version

print "Loaded Word, version {$word->Version}<BR>";

#Another way to get the version using com_get

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

print "Version using Com_get(): $testversion <BR>";

#Make it visible in a window

$word->Visible = 1;

#Open a new document

$word->Documents->Add();

#Write something

$word->Selection->TypeText("This is a test...");

#Now save the document

$word->Documents[1]->SaveAs("Useless test.doc");

#Comment next line if you want to see the word document,
#then close word manually

$word->Quit();
#Comment if you want to see the word document, then close

?>
If you study this example for a few minutes using the OLE documentation that comes with Word, you will learn practically all you need to write your own program.

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网