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;
}
表格网站php源码
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⽂件解压出来,直接查看⾥⾯的相关⽂件。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。