python word 合并单元格_PHPWord生成word实现table合并(colspan和rowspan)

PHPWord可以让php操作word文档了这个与phpexcel是一样的一个第三方插件了,我们下面来看在使用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.jquerycn.cn');

$table->addRow();

$table->addCell(100)->addText('PHP');

$table->addCell(100)->addText('python');

$table->addCell(100)->addText('java');

$section->addTextBreak(10);

生成word效果图如下:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值