| 如果你是一名冒险份子,而且你正在使用 cgi、isapi 或 apache 模块版本的 windows 系统上运行着php,那么你也可以获得系统的 com 功能。现在,解释 com(微软的组件对象模型)的工作留给了微软和那些大部头的图书来完成。然而,知道点 com 也没什么错,下面有一个普通的(没有双关语,针对很普通)代码小片断。
这代码小片断使用 php 在后台启动 microsoft word、打开一个新文件、键入一些文本、保存该文件然后关闭应用程序:
<? //create a reference to a new com component (word) $word = new com("word.application") or die("can't start word!");
//print the version of word that's now in use echo "loading word,v.{$word->version}<br />";
//set the visibility of the application to 0 (false) //to open the application in the forefront, use 1 (true)
$word->visible = 0;
//create a new document in word $word->documents->add();
//add text to the new document $word->selection->typetext("testing 1-2-3...");
//save the document in the windows temp directory $word->documents[1]->saveas("windowstempcomtest.doc");
//close the connection to the com component $word->quit();
//print another message to the screen echo "check for the file..." ?>
假设你正在运行一个内联网 web 站点,该站点把数据存放在 microsoft sql server 数据库内,你的用户需要 excel 格式的数据。那么,你可以让 php 执行必要的 sql 查询并且格式化输出结果,然后使用 com 启动 excel,把数据传输给它,最后再把文件存储到用户的桌面系统内。

|