• phpword读取内容和样式 生成新的内容


    table样式还未读出 正在测试中,
    目前有 rows cell textrun等样式
    顺序不固定 可以设定

    <?php
    require 'vendor/autoload.php';
    use PhpOfficePhpWordPhpWord;
    use PhpOfficePhpWordIOFactory;
    use PhpOfficePhpWordStyleFont;
    use PhpOfficePhpWordSharedipArchive;
    use PhpOfficePhpWordSettings;
    use PhpOfficePhpWordSharedConverter;
    use PhpOfficePhpWordStyleTablePosition;
    
    class Test extends MY_Controller
    {
    
    
        public $currentPage=0;
        private $args = null; // 文本段样式
    
        public  function __construct()
        {
            parent::__construct();
        }
    
        
       
        public function joinFile()
        {
            $page=2;
            $file1='/home/shaonianlang/桌面/111.docx';
            $file2='/home/shaonianlang/桌面/szxm.docx';
            $phpWord = new PhpWord();
            $S1 =IOFactory::load($file1)->getSections();
            $S2 = IOFactory::load($file2);
            $arr=[];
           
            
            foreach ($S1 as $S) {
                
                $elements = $S->getElements();
               
                // 逐级读取/写入节点
                $arr=$this->copyElement($elements, $section);
            }
    
           
            //合并文档
            //$section = $S2->addSection();
            //生成新文档
            $section = $phpWord->addSection();
           
            $table = $section->addTable();
            //$header = array('size' => 16, 'bold' => true);
            //table行数
            $rows = $arr['rows'];
            //table列数
            $cols = $arr['cells'];
            
            
            foreach($arr['tmptext'] as $qa) {
               
                $xas='';
                $header=[];
                if(is_array($qa)) {
                foreach ($qa as $axz) {
    
                $xas.=$axz['text'];
                $header=array('size'=>$axz['style']['basic']['size'],'name'=>$axz['style']['basic']['name']);
                }
               
                $section->addText($xas, $header);
            }
                
            }
    
            //var_dump($arr);
            
            //添加标题 后面是个央视
            
            //添加一个表格实力恶化
            //$table = $section->addTable();
            //行数循环
            for ($r = 1; $r <= $rows; $r++) {
                //添加行
                $table->addRow($arr['height'][$r]);
                //添加列数
                for ($c = 1; $c <= $cols; $c++) {
    
                    //判断当前列数中textrun的个数
                    $counts=count($arr['text'][$r][$c]);
                    
                   
                    //textrun表示就是一行
                    if($counts<2) {
                        $headers=array('size'=>$arr['text'][$r][$c][0]['style']['basic']['size'],'name'=>$arr['text'][$r][$c][0]['style']['basic']['name']);
                        $table->addCell($arr['width'][$r][$c])->addText($arr['text'][$r][$c][0]['text'],$headers);
                    }else {
                        //textrun表示是两行 完美
                        $texs='';
                        foreach($arr['text'][$r][$c] as $ka=>$txa) {
                            $texs.=$txa['text'];
                            $headers=array('size'=>$txa['style']['basic']['size'],'name'=>$txa['style']['basic']['name']);
                        } 
    
                        //将数据插入到表格中
                        $table->addCell($arr['width'][$r][$c])->addText($texs,$headers);
                    }
                       
                }
                    
                
            }
    
            
    
            $F1 = IOFactory::createWriter($phpWord);
            $path = APPPATH.'app/upload';
            if(!is_dir($path)) mkdir($path);
            $filePath = $path . time() . '.docx';
            $F1->save($filePath);
    
         
        }
    
        /**
         * 逐级读取/写入节点
         *
         * @param Array
         *    需要读取的节点
         * @param PhpOfficePhpWordElementSection
         *    节点的容器
         * @param Array
         *    文档2的所有节点
         */
        private function copyElement($elements, &$container, $S2 = null)
        {
            $inEls = [];
            $arrx=[];
            $styles=[];
            foreach ($elements as $e1) {
    
                $class=get_class($e1); //获取word中对应内容类型类
                $elname=explode("\", $class)[3];
    
                //$fun = 'add' . $elname;
    
                //如果没找到分页 则默认为第一页
                if ($elname == 'PageBreak') {
                    $this->currentPage++;
                }else{
                     $this->currentPage=1;
                }
    
                //文本节点 继续循环
                if ($elname=='TextRun') {
                    $arrx['tmptext'][]=$this->getTextElement($e1);//获取text节点内容
    
                    
                    
                }
    
                
                if ($elname=='Table') {
                     
                    $rows=count($e1->getRows()); //行数
                    $cells=$e1->countColumns(); //列数
                    //$style=$e->getStyle(); //表格样式
                    //$width=$e->getWidth(); //宽度
                    $arrx['rows']=$rows;
                    $arrx['cells']=$cells;
    
                    for($i=0;$i<$rows;$i++) {
                        $rows_a=$e1->getRows()[$i];
                        $arrx['height'][$i+1]=$rows_a->getHeight();
                        //$arrx['rows'][$i+1]=$this->rowstyle($rows_a);
                        for($j = 0; $j < $cells; $j++) {
                            //$arrx[$i][$r]=
                            $x=$rows_a->getCells()[$j];
                            $arrx['width'][$i+1][$j+1]=$x->getWidth();
                            $arrx['text'][$i+1][$j+1]=$this->getTextElement($x);
                            //$arrx['cells'][$i+1][$j+1]=$this->cellsstyle($x);
                        }
                    }
                    
                }
               
            }
    
            return $arrx;
        }
    
       
    
        /**
         * 获取Text节点
         */
        private function getTextElement($E)
        {
            $elements = $E->getElements();
            $xas='';
            $result = [];
            $inResult=[];
            $text=[];
            foreach($elements as $inE) {
                $ns = get_class($inE);
                $elName = explode('\', $ns)[3];
                if($elName == 'Text') {
                    
                    $result[] = $this->textarr($inE);
                    
                    
                    
                        
                } elseif (method_exists($inE, 'getElements')) {
                    $inResult = $this->getTextElement($inE);
                }
                if(!is_null($inResult)) {
                    $result = array_merge($result, $inResult);
                }
            }
    
            // if (count($result)>0) {
            //     foreach($result as $xaa) {
            //        var_dump($xaa);
            //     }
            // }
            
            
            return count($result) > 0 ? $result : null;
        }
    
        /**
         * 获取text的样式
         *
         * @param [type] $e
         * @return void
         */
        public function style($e) 
        {
            $style=$e->getFontStyle();
            $arry=$style->getStyleValues();
            foreach($arry as $key=>$xas) {
                if (is_object($xas)) {
                    $arry['Paragraph'][]=$xas->getStyleValues();
                    unset($arry[$key]);
                    
                }
            }
    
          return $arry;
        }
    
        /**
         * 行样式
         *
         * @param [type] $rows
         * @return void
         */
        public function rowstyle($rows)
        {
            $style=$rows->getStyle();
            
            $arry['TblHeader']=$style->getTblHeader();
            $arry['CantSplit']=$style->getCantSplit();
            $arry['ExactHeight']=$style->getExactHeight();
    
            return $arry;
        
        }
    
        /**
         * 列的样式
         *
         * @param [type] $cells
         * @return void
         */
        public function cellsstyle($cells)
        {
            $style=$cells->getStyle();
            $arry['VAlign']=$cells->getVAlign();
            $arry['TextDirection']=$cells->getTextDirection();
            $arry['BgColor']=$cells->getBgColor();
            $arry['GridSpan']=$cells->getGridSpan();
            $arry['VMerge']=$cells->getVMerge();
            $arry['Shading']=$cells->getShading();
            $arry['Width']=$cells->getWidth();
            $arry['Unit']=$cells->getUnit();
            $arry['DefaultBorderColor']=$cells->getDefaultBorderColor();
    
            return $arry;
        }
    
        public function textarr($e)
        {
            $textArr['text']=$e->getText();
            $textArr['style']=$this->style($e);
            
            return $textArr;
        }
    
        
    
       
    
           
        
    }
    
  • 相关阅读:
    2015 HUAS Summer Contest#2~B
    2015 HUAS Summer Contest#2~A
    HUAS Summer Trainning #3~B
    HUAS Summer Trainning #3~A
    2015 HUAS Provincial Select Contest #1~D
    UVA 725
    货币体系
    N皇后摆放问题
    种子填充找连通块 floodfill
    二叉树的递归遍历,用先序和中序输出后序
  • 原文地址:https://www.cnblogs.com/mengluo/p/10280381.html
Copyright © 2020-2023  润新知