当前位置: 首页 > 后端技术 > PHP

使用PHPWord合并Word文档,在文档的指定页面插入另一个文档的内容

时间:2023-03-29 16:24:36 PHP

提示:不支持.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+1getFontStyle();}else{继续;}$nextEls=$this->getTextElement($nextE);如果(is_null($nextEls)){$nextStyle=newFont();}else{$nextStyle=current($nextEls)->getFontStyle();}}}#设置参数$a=$b=$c=$d=$e=null;@list($a,$b,$c,$d,$e)=$args;$newEl=$container->$fun($a,$b,$c,$d,$e);$this->setAttr($elName,$newEl,$E);#$inEls=[];如果(method_exists($E,'getElements')&&$elName!='TextRun'){$inEls=$E->getElements();}if(method_exists($E,'getRows'))$inEls=$E->getRows();如果(method_exists($E,'getCells'))$inEls=$E->getCells();如果(count($inEls)>0){$this->copyElement($inEls,$newEl);$inEls=[];$this->args=null;}}返回$pageIndex;}/***获取文本节点*/privatefunctiongetTextElement($E){$elements=$E->getElements();$结果=[];foreach($elementsas$inE){$ns=get_class($inE);$elName=end(explode('\\',$ns));if($elName=='Text'){$inE->setPhpWord(null);$结果[]=$inE;}elseif(method_exists($inE,'getE元素')){$inResult=$this->getTextElement($inE);}if(!is_null($inResult))$result=array_merge($result,$inResult);}返回计数($result)>0?$结果:空;}privatefunctionsetAttr($elName,&$newEl,$E){switch(strtolower($elName)){case'脚注':$newEl->setReferenceId($E->getReferenceId());休息;case'formfield':$newEl->setName($E->getName());$newEl->setDefault($E->getDefault());$newEl->setValue($E->getValue());$newEl->setEntries($E->getEntries());休息;case'object':$newEl->setImageRelationId($E->getImageRelationId());$newEl->setObjectId($E->getObjectId());休息;案例'sdt':$newEl->setValue($E->getValue());$newEl->setListItems($E->getListItems());休息;case'table':$newEl->setWidth($E->getWidth());休息;}}}程序员客栈,汇聚各行各业的coder,寻找你靠谱的技术伙伴http://t.cn/RXz4ONT