您现在的位置是:首页 > 开发开发

php_com_dotnet DCOM读取合并word文档

谜团 2020-07-05 00:00:00【开发】人已围观

简介php读取多个word,并进行合并保存,采用php_com_dotnet来完成,服务器需要安装word

//错误捕捉,实际应用中去除
function shutdown_function()  
{  
    $e = error_get_last();  
 
    
    var_dump(iconv('gbk','utf-8',$e['message']));
}
register_shutdown_function('shutdown_function');  
//实际代码从这里开始
$word = new COM("Word.Application"or die("Can't start Word!");
echo "Loading Word, v{$word->Version}</br>";
$word->Visible = false;
//$word->Documents->Open($file) or die ("Unable to open File:".$file);
// //读取文档内容
 //$test= $word->ActiveDocument->content;
// $word->Documents->open(dirname(__FILE__).'\6.docx');
// //$test.= $word->ActiveDocument->content->Text;
// $test.= $word->ActiveDocument->content;
 
// $word->Selection->WholeStory();//全选
// $word->Selection->Copy();//复制
// $word->ActiveDocument->Close(false);
 
$word->Documents->Add();
// // //在新文档中添加文字 
// $word->Selection->Paste();//粘贴
// $word->Selection->Delete();删除回车符
 
$file=iconv('utf-8','gbk','周报表.docx');
$file1=iconv('utf-8','gbk','12-周报表.docx');
$word->Selection->InsertFile($file);
$word->Selection->InsertFile($file1);
// $word->Selection->InsertFile('11-周报表.docx');//插入文件
// $word->Selection->InsertFile('12-周报表.docx');//插入文件
//$word->Selection->TypeText("$test");  
$word->Documents[1]->SaveAs($newfile);
//关闭与COM组件之间的连接  
$word->ActiveDocument->Close(false);
$word->Quit();
//$word->Release();
$word = null;
unset($word);
准备工作:
php.ini启用php_com_dotnet
com.allow_dcom = true

extension=php_com_dotnet.dll

 

仅适用于windows服务器,服务器要安装word并配置dcom
服务器DCOM设置 

很赞哦! ()

上一篇:DCOM配置word

下一篇:PHP Switch 语句

文章评论

热评榜