haidong 发表于 2010-2-24 13:33:48

PHP脚本的10个技巧(6)

<br>                   PHP和COM<BR><BR>  如果你是一名冒险份子,而且你正在使用CGI、ISAPI或Apache模块版本的Windows系统上运行着PHP,那么你也可以获得系统的COM功能。现在,解释COM(微软的组件对象模型)的工作留给了微软和那些大部头的图书来完成。然而,知道点COM也没什么错,下面有一个普通的(没有双关语,针对很普通)代码小片断。<BR><BR>这代码小片断使用PHP在后台启动Microsoft Word、打开一个新文件、键入一些文本、保存该文件然后关闭应用程序:<BR><BR>// create a reference to a new COM component (Word)<BR>$word = new COM("word.application") or die("Can't start Word!");<BR><BR>// print the version of Word that's now in use<BR>echo "Loading Word, v. {$word-&gt;Version} <BR>  ";<BR><BR>// set the visibility of the application to 0 (false)<BR>// to open the application in the forefront, use 1 (true)<BR>$word-&gt;Visible = 0;<BR><BR>// create a new document in Word<BR>$word-&gt;Documents-&gt;Add();<BR><BR>// add text to the new document<BR>$word-&gt;Selection-&gt;TypeText("Testing 1-2-3...");<BR><BR>//save the document in the Windows temp directory<BR>$word-&gt;Documents-&gt;SaveAs("/Windows/temp/comtest.doc");<BR><BR>// close the connection to the COM component<BR>$word-&gt;Quit();<BR><BR>// print another message to the screen<BR>echo "Check for the file...";<BR>?&gt;<BR><BR>假设你正在运行一个内联网Web站点,该站点把数据存放在Microsoft SQL Server数据库内,你的用户需要Excel格式的数据。那么,你可以让PHP执行必要的SQL查询并且格式化输出结果,然后使用COM启动Excel,把数据传输给它,最后再把文件存储到用户的桌面系统内。                   <br><br>
页: [1]
查看完整版本: PHP脚本的10个技巧(6)