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



    

    

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值