phpword合并单元格后,office或者wps下表格无法正常显示

    phpword在使用单元格合并时,基本操作都一样,可以看连接:https://blog.csdn.net/yellowxiaotian/article/details/20369597。

    一般来说都是需要计算好单元格宽度的,但是,有时候在office中正常,但是到wps中不正常,或者wps中正常office中不正常,具体表现为:在一方里表格内单元格宽度按照计算全部贴合,但是在另一方里有些行始终对不齐。

    造成这个问题原因:没有按照office的正确格式设置合并单元格。

    解决办法:

    1、/PHPWord/Style/Cell.php中,增加以下部分

private $_rowMerge = null;  
private $_cellMerge = null;  
private $_gridSpan = 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;  
}  
public function getGridSpan(){
		return $this->_gridSpan;
	}


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

$rowMerge = $style->getRowMerge();
$cellMerge = $style->getCellMerge();
$gridSpan = $style->getGridSpan();
$styles = (!is_null($bgColor) || !is_null($valign) || !is_null($textDir) || $borders || !is_null($rowMerge) || !is_null($cellMerge)) ? true : false;

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

if (!is_null($cellMerge)) {
	$objWriter->startElement('w:gridSpan');
//	$objWriter->startElement('w:hMerge'); 注意这一行不要用
	if ((string)$cellMerge !== 'continue')
	{
//		$objWriter->writeAttribute('w:val', $cellMerge); 注意这一行不要用
		$objWriter->writeAttribute('w:val', $gridSpan);
	}
	$objWriter->endElement();
}

if (!is_null($rowMerge)) {
	$objWriter->startElement('w:vMerge');
	if ((string)$rowMerge !== 'continue')
	{
		$objWriter->writeAttribute('w:val', $rowMerge);
	}
	$objWriter->endElement();
}

扫描二维码关注公众号,回复: 2316263 查看本文章


    4、使用

$table->addRow(800);
$table->addCell(1000, $styleCell)->addText('问题标题');
$table->addCell(7800, array_merge($styleCell, array('cellMerge' => 'restart','gridSpan'=>3)))->addText($info['title']);
//  $table->addCell(2600, array_merge($styleCell, array('cellMerge' => 'continue')));
//  $table->addCell(2600, array_merge($styleCell, array('cellMerge' => 'continue')));



    

    

猜你喜欢

转载自blog.csdn.net/lz610756247/article/details/80163086