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

window|word

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.

时间: 2024-08-03 19:55:16

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

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

window|word Using the PHP4 COM functions with MS ExcelAs for the Word example above, study the code with the help from the Visual Basic Editor ObjectBrowser for Excel. <?php #Set the workbook to use and its sheet. In this example we use a spreadsheet

数据恢复-Windows 7 上删除的word文档恢复方法?

问题描述 Windows 7 上删除的word文档恢复方法? 随着计算机不断融入到人们的工作中,Word文档已经成为人们记录和存储文字信息的重要途径.然而,如果你Windows 7上记录着重要资料的Word文档不幸被误删除了,那么要怎样才能通过及时有效的Word文档恢复工作,最大限度的恢复被删除Word文档呢?面对这个问题,相信不少用户首先想到的就是Windows 7系统回收站.原因很简单,如果大家使用"Delete"命令在Windows 7上删除Word文档,那么这些被删除Word文

使用php创建word文档的例子

例子  代码如下 复制代码 <?php include("word.php"); $word=new word; $word->start(); ?> <title>直接用php创建word文档</title>  <h1>直接用php创建word文档</h1>  作者:axgle <hr size=1>  <p>如果你打开word.doc,看到了这里的介绍,则说明word文档创建成功了. <

word文档都能带毒 可攻击Windows和macOS

一般来讲,网络攻击最多的可能来自于网页或者链接,可是近日在网上出现了利用宏功能漏洞来携带恶意程序的word文档,当使用Windows或者macOS设备打开后就可能会自动下载一些恶意的程序.   据外媒报道,Office办公套件中的宏功能经常会被攻击者利用,一些黑客常会用其入侵或感染系统,而这种新型的攻击手段就是利用了这种手段,基于VBA(Visual basic for Applications)代码来部署恶意程序. 与普通的攻击不同,这种攻击不仅能对PC端的Windows系统进行攻击,也可以对

简述PHP4和PHP5版本下解析XML文档的操作方法

在PHP网站开发与建设过程中,时常会碰到需要对XML文档进行解析,PHP4版本自带了XML解析器(sax),PHP5版本增加了SimpleXML(基于dom)的XML扩展,对XML的解析更是非常方便,今天和大家分享下在不同环境下对XML文档进行解析的方法.XML文档 1234567891011121314151617181920 <?xml version="1.0" encoding="gbk"?> <LeapsoulXML>      

怎么样使用java生成一个word文档,求个例子,word的格式可以自己定义和排版。

问题描述 类似LoadRunner导出测试报告的那种功能,希望大家可以提供些技术,小弟在此谢过大家了. 解决方案 解决方案二:利用流,输出流,先创建文件,然后利用输出流输出到word文档中.以下程序仅供参考://建立一个文件,向其中写入多行数据,然后读出来打印到显示器上importjava.io.*;importjava.util.*;classTestBufferedOutputStream{publicstaticvoidmain(Stringargs[])throwsIOException

将HTML文档设置为Windows桌面背景

  从Windows 3.X开始,系统就提供了设置桌面背景的功能,我们可以将自己喜爱的图片作为背景显示在桌面上,从而美化了用户的工作环境,因而深受广大用户的欢迎.不过在老版本的Windows中,背景功能仅仅只是一个装饰品,它并不能给用户带来多大的实际价值,而到了Windows 98种情况就发生了变化.Windows 98新增了Web显示功能,我们可利用它功能将一个活动的HTML文档设置为Windows 98的桌面背景,此后我们就可直接利用设置在桌面上的Internet连接查看有关网络信息(也就是

转帖:PHP4(windows版本)中的COM函数

window|函数 这几天一直在写excel转化成mysql,发现一篇文章,搜一下phpx论坛,没有这个帖子,把它转帖如下: PHP4(windows版本)中的COM函数 介绍 内置于PHP4里的COM函数对于我们在win32环境下开发程序是相当有吸引力的,但是至今仍没有多少相关的技术文档.本文将以三个例子分 别处理 MS office 2000 Word . Excel . Adobe Distiller 来说明如何在PHP中使用COM函数. COM技术是由Microsoft在几年前提出并开发

PHP4(windows版本)中的COM函数

window|函数 介绍 内置于PHP4里的COM函数对于我们在win32环境下开发程序是相当有吸引力的,但是至今仍没有多少相关的技术文档.本文将以三个例子分别处理 MS office 2000 Word . Excel . Adobe Distiller 来说明如何在PHP中使用COM函数. COM技术是由Microsoft在几年前提出并开发的,本文中提到的相关名词有OLE, OLE Automation, ActiveX, COM ,这些词的意思都基本一样,都表示用一段封装的代码(对象)来完