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

window|word

Using the PHP4 COM functions with MS Excel
As 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 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 that 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 will 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 see 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 use of the built-in functions in Excel:
#Function: 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 $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 example should get you going with the Excel COM and PHP. Of course there are many more objects to use. Writing an OOP wrapper for the principal functions will make access to the excel objects even easier.
Using PHP COM with Adobe Distiller
This last example is for a non-MS program: If your program has produced a PostScript document, it may be interesting to transform it (Distill it) to a PDF document. Adobe has a program called Distiller with a windows version that can be instantiated, with the following code:
<?php

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

?>
Note that the OLE Identifier name is not obvious, especially when the distiller documentation (Adobe's Technical Note #5158) 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 is 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 use the default Job options file. For example:
<?php

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

?>
There are more methods and properties that can be used with Distiller. If you are interested, look at the Adobe's technical note.
Caveats/Possible problems
If there are some errors in your code, you may instantiate the object and your program may not close before it times out. Worst of all, the application may retentively be instantiated. As a result, several copies may lay around in your programs list and interfere after you have corrected the problem. The solution: After fixing the bug, clean up (<CTRL+ALT+Delete> and End Task) all the instances in the program list before you restart. For this same reason, always close the application at the end of your code and unlink the instance.
You may experience some oddities with com_get and com_set. For example: $Version = Com_get($instance->Application,"Version"); Works with Word, but produces an error with Excel.
Some Objects won't be instantiated by PHP4, it appears that these objects need a custom interface that PHP4-COM doesn't support.
Why use it?
Hopefully, these 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 be integrated with the rest of PHP's powerful database functions. Microsoft markets the COM technology everywhere and under different names and architectures, like COM+(Combine COM with Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, etc. PHP and Apache, working together, are now offering an 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:

时间: 2024-09-20 15:07:15

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

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

window|word COM Functions in PHP4 (Windows)Alain M. Samoun IntroductionThe 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

数据恢复-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 ,这些词的意思都基本一样,都表示用一段封装的代码(对象)来完