Use PHPWord to generate word to merge tables (colspan and rowspan)

Source: Internet
Author: User
Tags phpword
PHPWord (http://phpword.codeplex.com/) is a good tool to process and generate word documents, but to generate complex word, such as the implementation of colspan and rowspan, you still need to make some modifications.

PHPWord (http://phpword.codeplex.com/) is a good tool to process and generate word documents, but to generate complex word, such as the implementation of colspan and rowspan, you still need to make some modifications.

Step 1: Add the following attributes to the phpword/Style/Cell. php file class:


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

Step 2: Add the following method to the phpword/Style/Cell. php file class:


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; }

Step 3: Add the following in phpword/Style/Cell. php file class constructor _ construct:


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

Step 4: Add the following content to the _ writeCellStyle method of the phpword/writer/word2007/base. php class:


$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. Congratulations, you have done it. then let's see how to use it!

Use of 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');

The generated word is as follows:

 

Use of PHPWord colspan:


$ Table-> addRow (); $ styleCell = array ('gridspan '=> 2); $ table-> addCell (200, $ styleCell) -> addText ('php point pass'); $ 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 );

The generated word is as follows:

Using PHPWord, I can easily generate complex words in projects. please leave a message if you have any questions!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.