提示:不支持.doc文件的读取。)到另一个文档(DOC2)的指定页面。由于两个文件的内容不固定,所以无法使用PHPWord的Template功能。之前没用过PHPWord,折腾了几天。从熟悉PHPWord的功能开始,看样例代码和源码,最后找出解决方法,下面说说解决方法:首先阅读DOC1的内容,PHPWord将内容划分到不同的节点和容器中,最上面的是Section,里面有TextRun(文本块)、Table(表格)等容器。这些容器中有Text(文本)、Row(表格行)、Cell(单元格)等,还有一些其他的制表符,如TextBreak、PageBreak(分页符)。一步步阅读完内容后,将阅读的内容插入到一个新文档中。读取指定的分页符后,读取DOC2的内容,将其与之前的内容插入,最后保存新文档。粘贴一部分代码:namespaceHome\Logic;Vendor('PhpOffice.autoload');usePhpOffice\PhpWord\PhpWord;usePhpOffice\PhpWord\IOFactory;usePhpOffice\PhpWord\Style\Font;usePhpOffice\PhpWord\Shared\压缩存档;使用PhpOffice\PhpWord\Settings;classMergeFile{private$currentPage=0;//当前页面private$page=0;//插入页码private$args=null;//文本部分样式private$tmpFiles=[];//临时文件/***合并文件**@paramURI*文件1的地址*@paramURI*文件2的地址*@paramNumeric*指定要插入的页数**@returnString*的URI新文件*/publicfunctionjoinFile($file1,$file2,$page){$S1=IOFactory::load($file1)->getSections();$S2=IOFactory::load($file2)->getSections();$this->page=$page>0?$页-1:$页;$phpWord=新的PhpWord();foreach($S1as$S){$section=$phpWord->addSection($S->getStyle());$元素=$S->获取元素();#逐步读/写节点$this->copyElement($elements,$section,$S2);$F1=IOFactory::createWriter($phpWord);$path=$_SERVER['DOCUMENT_ROOT']。__根__。'/公开/写/';如果(!is_dir($path))mkdir($path);$文件路径=$路径。时间()。'.docx';$F1->保存($文件路径);#清除临时文件foreach($this->tmpFilesas$P){unlink($P);}返回$文件路径;}/***逐级读/写节点**@paramArray*要读取的节点*@paramPhpOffice\PhpWord\Element\Section*节点容器*@paramArray*文档2的所有节点*/privatefunctioncopyElement($elements,&$container,$S2=null){$inEls=[];foreach($elementsas$i=>$E){#检查当前页码if($this->currentPage==$this->page&&!is_null($S2)){#开始插入foreach($S2as$k=>$v){$ELS=$v->getElements();$this->copyElement($ELS,$container);}#清除以防止重复插入$S2=null;}$ns=get_class($E);$elName=end(explode('\\',$ns));$乐趣='添加'。$elName;#计算页数if($elName=='PageBreak'){$this->currentPage++;}#合并文本段if($elName=='TextRun'#&&!is_null($S2)){$tmpEls=$this->getTextElement($E);如果(!is_null($tmpEls)){$inEls=array_merge($inEls,$tmpEls);}$nextElName='';如果($i+1
