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文件解压出来,直接查看里面的相关文件。

你可能感兴趣的:(php)