PHPWord 表格居中和合并单元格
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 start*******************/ $styleTable = array('borderSize'=>6, 'borderColor'=>'000000', 'cellMargin'=>80); $styleCell = array('valign'=>'center', 'align'=>'center'); $fontStyle = array('size'=>10 /*,'bold'=>true*/); $PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $fontStyle); $table = $section->addTable('myOwnTableStyle'); $table->addRow(); $table->addCell(1600)->addText('分类', $fontStyle, $styleCell); $table->addCell(1600)->addText('序号', $fontStyle, $styleCell); $table->addCell(1600)->addText('制作项目', $fontStyle, $styleCell); $table->addCell(2000)->addText('制作说明', $fontStyle, $styleCell); $table->addCell(1600)->addText('采用技术', $fontStyle, $styleCell); $table->addCell(1600)->addText('报价', $fontStyle, $styleCell); $table->addCell(1600)->addText('数量', $fontStyle, $styleCell); $table->addCell(1600)->addText('总价', $fontStyle, $styleCell); $table->addCell(2600)->addText('备注', $fontStyle, $styleCell); $table->addRow(); $table->addCell(600, array('rowMerge' => 'restart', 'valign' => "center"))->addText('内容部分'); $table->addCell(1600)->addText('1', $fontStyle, $styleCell); $table->addCell(1600)->addText('片头LOGO演绎', $fontStyle, $styleCell); $table->addCell(2000)->addText('10秒左右的视频制作', $fontStyle, $styleCell); $table->addCell(1600)->addText('AE', $fontStyle, $styleCell); $table->addCell(1600)->addText('2000元/个', $fontStyle, $styleCell); $table->addCell(1600)->addText('1', $fontStyle, $styleCell); $table->addCell(1600)->addText('', $fontStyle, $styleCell); $table->addCell(2600)->addText('', $fontStyle, $styleCell); $table->addRow(); $table->addCell(1600, array('rowMerge' => 'continue')); $table->addCell(1600)->addText('2', $fontStyle, $styleCell); $table->addCell(1600)->addText('UI界面设计', $fontStyle, $styleCell); $table->addCell(2000)->addText('程序框架设计及动态交互设计', $fontStyle, $styleCell); $table->addCell(1600)->addText('AI/PS', $fontStyle, $styleCell); $table->addCell(1600)->addText('7000元/套', $fontStyle, $styleCell); $table->addCell(1600)->addText('1', $fontStyle, $styleCell); $table->addCell(1600)->addText('', $fontStyle, $styleCell); $table->addCell(2600)->addText('', $fontStyle, $styleCell); $table->addRow(); $table->addCell(1600, array('rowMerge' => 'continue')); $table->addCell(1600)->addText('3', $fontStyle, $styleCell); $table->addCell(1600)->addText('文案创意撰写', $fontStyle, $styleCell); $table->addCell(2000)->addText('系统中要表现的项目内容的创意和文案撰写', $fontStyle, $styleCell); $table->addCell(1600)->addText('文案策划创意', $fontStyle, $styleCell); $table->addCell(1600)->addText('6000元/套', $fontStyle, $styleCell); $table->addCell(1600)->addText('1', $fontStyle, $styleCell); $table->addCell(1600)->addText('', $fontStyle, $styleCell); $table->addCell(2600)->addText('建议不做', $fontStyle, $styleCell); $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'));
转载自:http://blog.csdn.net/yellowxiaotian/article/details/20369597