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();
}


    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')));



    

    


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部