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即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梧桐深院

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值