PHPWord產生word實現table合并(colspan和rowspan)

來源:互聯網
上載者:User

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.111cn.net');
            $table->addRow();
            $table->addCell(100)->addText('PHP');
            $table->addCell(100)->addText('python');
            $table->addCell(100)->addText('java');
            $section->addTextBreak(10);


產生word效果圖如下:

psps6s.png" />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.