[php]在PHP中读取和写入WORD文档的代码


测试平台windows 

使用的windows的com主键。

 1 <?  
 2 // 建立一个指向新COM组件的索引  
 3 $word = new COM("word.application") or die("Can't start Word!");  
 4 // 显示目前正在使用的Word的版本号  
 5 //echo "Loading Word, v. {$word->Version}<br>";  
 6 // 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)  
 7 // to open the application in the forefront, use 1 (true)  
 8 //$word->Visible = 0;  
 9 
10 //打?一个文档  
11 $word->Documents->OPen("d:\a.doc");  
12 //读取文档内容  
13 
14 $test= $word->ActiveDocument->content->Text;  
15 
16 echo $test;  
17 echo "<br>";  
18 //将文档中需要换的变量更换一下  
19 $test=str_replace("<{变量}>","这是变量",$test);  
20 echo $test;  
21 $word->Documents->Add();  
22 // 在新文档中添加文字  
23 $word->Selection->TypeText("$test");  
24 //把文档保存在目录中  
25 $word->Documents[1]->SaveAs("d:/myweb/comtest.doc");  
26 // 关闭与COM组件之间的连接  
27 $word->Quit();  
28 ?> 

 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM