How to merge cells in PHPWord? Or to have different number of cells in each table row?

拟墨画扇 提交于 2020-02-22 12:50:08

问题


Trying to create MS Word document using PHP library PHPWord.

Can you help me how to merge two or more cells in document? Or how to have different number of cells in each table row?

I tried with cell definition as:

$cell11 = array('gridSpan'=>2);

But it did not worked....

Thank you in advance!


回答1:


Add code like this

1. PHPWord/Section/Table/Cell.php

/**
 * How many columns this cell spans
 * @var int
 */
private $_gridSpan;

public function__construct($insideOf,$pCount,$width=null,$style=null,$gridSpan=1){
    $this->_insideOf = $insideOf;
    ...
    $this->_gridSpan = $gridSpan;
}

/**
 * Get the number of columns this cell spans
 *
 * @return int
 */
public function getGridSpan(){
    return $this->_gridSpan;
}

2. PHPWord/Section/Table.php

     public function addCell($width,$style=null,$gridSpan=1){
            $cell = new PHPWord_Section_Table_Cell($this->_insideOf,
                    $this->_pCount,$width,$style,$gridSpan);
      }

3. PHPWord/Writer/Word2007/Base.php

protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
    $_rows = $table->getRows();
    $_cRows = count($_rows);
    ...
    $_heights = $table->getRowHeights();
    for ($i = 0; $i < $_cRows; $i++) {
        $row = $_rows[$i];
        $height = $_heights[$i];
        //add
        $objWriter->startElement('w:trPr');
        //FIXME: Make this an option on a row or table
        $objWriter->startElement('w:cantSplit');
        $objWriter->endElement();
        $objWriter->endElement();
        //end add
        foreach ($rows as $cell) {
            ...
            $width = $cell->getWidth();
            //add 
            $gridSpan = $cell->getGridSpan();
            //end add
            ...
            //add
            if ($gridSpan > 1) {
                $objWriter->startElement('w:gridSpan');
                $objWriter->writeAttribute('w:val',$gridSpan);
                $objWriter->endElement();
                //end add
                ...
            }
        }
    }
}

You can use it like this:

$PHPWord  = new PHPWord();
$section  = $PHPWord->createSection();
$table    = $section->addTable();
$table->addRow();
$table->addCell(1000,null,2)->addText('info');

$table->addRow();
$table->addCell(500)->addText('Name');
$table->addCell(500)->addText('Sex');

Sorry about the style,you can use it like this:

$tableStyle = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$PHPWord->addTableStyle('tableStyle',$tableStyle,null);
$table    = $section->addTable('tableStyle');
$table->addRow();
$table->addCell(1000,null,2)->addText('info',null,array('align'=>'center'));

$table->addRow();
$table->addCell(500)->addText('Name',null,array('align'=>'center'));
$table->addCell(500)->addText('Sex',null,array('align'=>'center'));


来源:https://stackoverflow.com/questions/11925337/how-to-merge-cells-in-phpword-or-to-have-different-number-of-cells-in-each-tabl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!