zoukankan      html  css  js  c++  java
  • php 使用 phpword 操作 word 读取 word

    思路 

    1. 加载word文件。
    2. 循环判断加载出来的数据。
    ( 数据下面有很多个节点 )
    ( 节点是按照数据的类型分类的 例如 无样式的文本是RunText,换行是TextBreak,表格是table.....等)
    3. 循环判断他们的数据类型是什么进行读取。
    4. 如果是文本的话就使用 节点->getText() 就直接可以获取到文本内容 表格的话有点麻烦。

    关于操作word的一些东西
    https://segmentfault.com/a/1190000019479817?utm_source=tag-newest
    https://www.cnblogs.com/mengluo/p/10280381.html(本博源于这个博客)



    代码
    // 准备条件 下载 phpword 的拓展库
    
    // 加载
    $source     = IOFactory::load($filePath)->getSections();
    
    foreach ($source as $S)
    {
                $elements = $S->getElements();
                if (!empty($this->GetElement($elements)))
                {
                    $arr = $this->GetElement($elements);
                    $this->todoGo($arr,$tableCatId,$tableStageId,$filePath,$versionId);
                }
    }
    
    // 逐级读取/读取节点
    function GetElement($elements)
    {
            $arrx=[];
            foreach ($elements as $k=>$e1)
            {
                // 获取word对象中对应内容类型类的节点的类名
                $class = $this->getClass($e1);
                if ($class=='Table')
                {
                     // 获取最大行
                     $rows=count($e1->getRows());
    
                     // 获取最大列
                     $cells=$e1->countColumns();
    
                     $arrx[$k]['rows']=$rows;
                     $arrx[$k]['cells']=$cells;
    
                     // 循环获取对应行和列下的单元格的文本内容
                     for($i=0;$i<$rows;$i++)
                     {
                          // 获取对应行
                          $rows_a=$e1->getRows()[$i];
                          for($j = 0; $j < $cells; $j++)
                          {
                               // 获取对应列
                               $x=$rows_a->getCells()[$j];
                               $arrx[$k]['text'][$i+1][$j+1]=$this->getTextElement($x);
                          }
                     }
                }
          }    
    }    
    
    //获取文本的节点
    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);
                }
            }
            return count($result) > 0 ? $result : null;
    }      
    
    //获取文本
    function textarr($e)
    {
          $textArr['text']=$e->getText();
         return $textArr;
    }     
    
    
    
     
  • 相关阅读:
    (判断是否为弱联通分量) poj 2762
    (最大生成树) poj 1979
    (暴力) bzoj 2208
    (BFS) bzoj 1102
    (并查集) bzoj 1161
    (数学) bzoj 1800
    (博弈) bzoj 2460
    (dinic) poj 3469
    (双端队列优化的SPFA) bzoj 2100
    (判断负环) bzoj 2019
  • 原文地址:https://www.cnblogs.com/lky-19990802/p/11326733.html
Copyright © 2011-2022 走看看