php com组件编辑word文档(中文不乱码)

1.检查php的ext目录下是否有php_com_dotnet.dll,没有的话百度下载与php对应版本的该文件放进去
2.php.ini中配置extension=php_com_dotnet以及com.allow_dcom = true
3.重启php
4.开始表演

简单编辑

$file = 'C:/Users/Administrator/Desktop/11.doc';
 $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
         // 是否显示word界面
        $word->Visible = 0;
        // 打开word文件
        $word->Documents->Open($file,true,false);
        // 获取文档内容
        $text= $word->ActiveDocument->content->Text;
        // 自己的处理逻辑
        $text = str_replace('张三','李四',$text);
        // 全选内容
        $word->ActiveDocument->Select();
        // 重新写入
        $word->Selection->TypeText($text);
        // 保存
        $word->ActiveDocument->save();
        // 关闭
        $word->Quit();
        $word = null;

查找替换

然而,上面这个代码有个致命的缺陷,那就是,当你编辑一个有格式的文档时,编辑后,格式都丢失了。如果只是简单的替换某些内容,我们可以使用查找替换功能,如下:

$file = 'C:/Users/Administrator/Desktop/11.doc';
        $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
        $word->Visible = 0;
        $word->Documents->Open($file,true,false);
        // 多种方式获取Find对象
        //$word->ActiveDocument->Select();
        //$Find = $word->Selection->Find;
        $Find = $word->ActiveDocument->content->Find;
        $Find->ClearFormatting();
                    //FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl
        $Find->Execute('张三',false,     false,          false,          false,           false,             true,     1,true,'张三丰',2);
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

按段落编辑

 $file = 'C:/Users/Administrator/Desktop/11.doc';
        $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
        $word->Visible = 0;
        $word->Documents->Open($file,true,false);
        $Find = $word->ActiveDocument->content->Find;
        $Find->MatchWildcards=true;
        $Find->Execute("张三");
        // 先查找一下,如果有再进行下面的循环操作
        if($Find->Found){
            $result = $Find->Parent->Text;
                $Paragraphs = $word->ActiveDocument->Paragraphs;
                for($i=1;$i<=$Paragraphs->Count;$i++){
                    $one = $Paragraphs->Item($i);
                    $Range = $one->Range;
                    // 用来判断该段落是否有需要需改的内容
                    $text = $Range->Text;
                    // 选中它 复制它的格式,修改它,再粘贴它的格式
                    if(strpos($text,'张三')!==false){
                        $Range->select();
                        $word->Selection->CopyFormat();
                        $word->Selection->TypeText('张三丰');
                        $word->Selection->PasteFormat();
                    }
                }
        }
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

查找的时候可能会用到通配符,参考这里:
搜索关键字:word wildcards
https://www.officetooltips.com/word_2016/tips/using_wildcards.html

https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference
我测试的时候用php7.4无法开启php_com_dotnet模块,报错:
PHP Warning: PHP Startup: Failed to load ext\php_com_dotnet
解决方案:php降级到7.2
电脑上安装了office word2007的不行,程序报错。
解决方案:安装wps即可

猜你喜欢

转载自blog.csdn.net/wang740209668/article/details/108377190
今日推荐