php word 合并单元格格式,PHPWord小记之表格居中和合并单元格

和上篇一样,文章基于PHPword Beta 0.6.3,可能有些功能在PHPWord后续版本已经修正。

1、表格对齐和表格缩进

/PHPWord/Style/TableFull.php添加相关属性和方法

// 表格居中属性,用法:表格style数组中:'alignMent' => 'center'

private $_alignMent = null;

// 表格缩进属性

private $_tableIndent = null;

public function setTableAlign($pValue = null)

{

$this->_alignMent = $pValue;

}

public function getTableAlign()

{

return $this->_alignMent;

}

public function getTableIndent()

{

return $this->_tableIndent;

}

public function setTableIndent($pValue = null)

{

$this->_tableIndent = $pValue;

return $this;

}构造函数中添加

unset($this->_firstRow->_alignMent);

unset($this->_firstRow->_tableIndent);

/PHPWord/Writer/Word2007/Styles.php大约144行_writeFullTableStyle方法内添加

$tableAlign = $style->getTableAlign();

$tableIndent = $style->getTableIndent();

同文件$objWriter->startElement('w:tblPr');下面

if ($tableAlign) {

$objWriter->startElement('w:jc');

$objWriter->writeAttribute('w:val', $tableAlign);

$objWriter->endElement();

}

if (!is_null($tableIndent))

{

$objWriter->startElement('w:tblInd');

if (!is_null($tableIndent))

{

$objWriter->writeAttribute('w:w', $tableIndent);

$objWriter->writeAttribute('w:type', 'dxa');

}

$objWriter->endElement(); // w:ind

}

这样就可以设置表格对齐方式和缩进了

$styleTable = array('borderSize'=>6, 'alignMent' => 'right', 'tableInden' => 1440);

$styleFirstRow = array('bgColor'=>'#1F497D');

$PHPWord->addTableStyle('tableStyle', $styleTable, $styleFirstRow);

$table = $section->addTable('tableStyle');

2、单元格合并

/PHPWord/Style/Cell.php添加属性和方法

private $_rowMerge = null;

private $_cellMerge = null;

public function getRowMerge()

{

return $this->_rowMerge;

}

public function setRowMerge($pValue = null)

{

$this->_rowMerge = $pValue;

return $this;

}

public function getCellMerge()

{

return $this->_cellMerge;

}

public function setCellValue($pValue = null)

{

$this->_cellMerge = $pValue;

return $this;

}

/PHPWord/Writer/Word2007/base.php中_writeCellStyle方法添加

$rowMerge = $style->getRowMerge();

$cellMerge = $style->getCellMerge();

在同方法中修改$styles(感谢网友提出,还真忘了加上这个),通过这个才能进入if ($styles)代码块里面:

$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir)

|| $borders || !is_null($rowMerge) || !is_null($cellMerge)) ? true : false;

在同方法if ($styles)中添加

if (!is_null($cellMerge))

{

//$objWriter->startElement('w:gridSpan');

$objWriter->startElement('w:hMerge');

if ((string)$cellMerge !== 'continue')

{

$objWriter->writeAttribute('w:val', $cellMerge);

}

$objWriter->endElement();

}

if (!is_null($rowMerge))

{

$objWriter->startElement('w:vMerge');

if ((string)$rowMerge !== 'continue')

{

$objWriter->writeAttribute('w:val', $rowMerge);

}

$objWriter->endElement();

}

使用方法:

$table->addRow(400);

$table->addCell(1600, array('cellMerge' => 'restart', 'valign' => "center"))->addText('横向合并');

$table->addCell(1600, array('cellMerge' => 'continue'));

$table->addCell(1600, array('cellMerge' => 'continue'));

$table->addCell(1600, array('cellMerge' => 'continue'));

附调试心得:如果生成的word文件和自己预想的样式不太一致,可以直接用解压缩软件如rar、zip打开word文件解压出来,直接查看里面的相关文件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值