PHPWord生成word實現table合並(colspan和rowspan)


PHPWord(http://phpword.codeplex.com/)是一個很好處理和生成WORD文檔的工具,但是生成復雜的word,如colspan和rowspan的實現,還是需要你做些修改。

第一步:在phpword/Style/Cell.php文件類中添加如下屬性:

private $_gridSpan;// for the colspan
private $_vMerge;// for the rowspan

第二步:在phpword/Style/Cell.php文件類中添加如下方法:

public function setGridSpan($pValue = null) 
{ 
   $this->_gridSpan = $pValue; 
} 
public function getGridSpan() 
{ 
   return $this->_gridSpan; 
}
public function setVMerge($pValue = null) 
{ 
   $this->_vMerge = $pValue; 
} 
public function getVMerge() 
{ 
   return $this->_vMerge; 
}


第三步:在phpword/Style/Cell.php文件類構造函數__construct()中添加如下:

$this->_gridSpan=null;
$this->_vMerge=null;

第四步:在phpword/writer/word2007/base.php類的_writeCellStyle方法中添加:

$gridSpan = $style->getGridSpan();
if(!is_null($gridSpan)) 
{ 
    $objWriter->startElement('w:gridSpan'); 
    $objWriter->writeAttribute('w:val', $gridSpan); 
    $objWriter->endElement(); 
}
/** edited by www.phpddt.com */
$vMerge = $style->getVMerge(); 
if(!is_null($vMerge)) 
{ 
    $objWriter->startElement('w:vMerge'); 
    $objWriter->writeAttribute('w:val', $vMerge); 
    $objWriter->endElement(); 
}

 

OK,恭喜你,搞定了,然后看看怎么使用吧!

PHPWord rowspan的使用:

$table = $section->addTable();
$table->addRow();
$table->addCell(100,array('vMerge' => 'restart'))->addText('1');
$table->addCell(100)->addText('2');
$table->addRow();
$table->addCell(100,array('vMerge' => 'fusion'));
$table->addCell(100)->addText('3');

 生成的word效果如下截圖:

 

PHPWord colspan的使用:

        $table->addRow();
        $styleCell=array('gridSpan' => 2);
        $table->addCell(200, $styleCell)->addText('PHP點點通');
        $table->addCell(100)->addText('http://www.phpddt.com');
        $table->addRow();
        $table->addCell(100)->addText('PHP');
        $table->addCell(100)->addText('python');
        $table->addCell(100)->addText('java');
        $section->addTextBreak(10);

生成word效果圖如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 table中tr間距的設定table合並單元格 colspan(跨列)和rowspan(跨行) Angular table 的 colspan rowspan 的動態綁定 jquery還原含有rowspan、colspan的table colspan="2"、列、rowspan="3"、行、用法! html表單元素的colspan和rowspan Extjs 實現多行合並(rowspan)效果實現二 PHPWord導出word文檔 PHPOffice下PHPWord生成Word2007(docx)使用方法
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM
標簽總結(colspan跨列 ,rowspan跨行) 使用PHPWORD 生成word文檔