table樣式還未讀出 正在測試中,
目前有 rows cell textrun等樣式
順序不固定 能夠設定php
<?php require 'vendor/autoload.php'; use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Shared\ZipArchive; use PhpOffice\PhpWord\Settings; use PhpOffice\PhpWord\Shared\Converter; use PhpOffice\PhpWord\Style\TablePosition; 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 PhpOffice\PhpWord\Element\Section * 節點的容器 * @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; } }