欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-28 13:36:34
复制代码 代码如下:

复制代码 代码如下:

<? 
// 建立一个指向新com组件的索引 
$word = new com(”word.application”) or die(”can't start word!”); 
// 显示目前正在使用的word的版本号 
//echo “loading word, v. {$word->version}<br>”; 
// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真) 
// to open the application in the forefront, use 1 (true) 
//$word->visible = 0; 

//打?一个文档 
$word->documents->open(”d:\myweb\muban.doc”); 
//读取文档内容 

$test= $word->activedocument->content->text; 

echo $test; 
echo “<br>”; 
//将文档中需要换的变量更换一下 
$test=str_replace(”<{变量}>”,”这是变量”,$test); 
echo $test; 
$word->documents->add(); 
// 在新文档中添加文字 
$word->selection->typetext(”$test”); 
//把文档保存在目录中 
$word->documents[1]->saveas(”d:/myweb/comtest.doc”); 
// 关闭与com组件之间的连接 
$word->quit(); 
?>