gpt4 book ai didi

phpword - 如何在 PHPWord 中合并单元格?或者在每个表格行中有不同数量的单元格?

转载 作者:行者123 更新时间:2023-12-02 22:32:58 27 4
gpt4 key购买 nike

尝试使用 PHP 库 PHPWord 创建 MS Word 文档。

你能帮我如何合并文档中的两个或多个单元格吗?或者如何在每个表格行中有不同数量的单元格?

我尝试将单元格定义为:

$cell11 = array('gridSpan'=>2);

但是没用....

提前致谢!

最佳答案

像这样添加代码

<强>1。 PHPWord/Section/Table/Cell.php

/**
* How many columns this cell spans
* @var int
*/
private $_gridSpan;

public function__construct($insideOf,$pCount,$width=null,$style=null,$gridSpan=1){
$this->_insideOf = $insideOf;
...
$this->_gridSpan = $gridSpan;
}

/**
* Get the number of columns this cell spans
*
* @return int
*/
public function getGridSpan(){
return $this->_gridSpan;
}

<强>2。 PHPWord/Section/Table.php

     public function addCell($width,$style=null,$gridSpan=1){
$cell = new PHPWord_Section_Table_Cell($this->_insideOf,
$this->_pCount,$width,$style,$gridSpan);
}

<强>3。 PHPWord/Writer/Word2007/Base.php

protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Table $table) {
$_rows = $table->getRows();
$_cRows = count($_rows);
...
$_heights = $table->getRowHeights();
for ($i = 0; $i < $_cRows; $i++) {
$row = $_rows[$i];
$height = $_heights[$i];
//add
$objWriter->startElement('w:trPr');
//FIXME: Make this an option on a row or table
$objWriter->startElement('w:cantSplit');
$objWriter->endElement();
$objWriter->endElement();
//end add
foreach ($rows as $cell) {
...
$width = $cell->getWidth();
//add
$gridSpan = $cell->getGridSpan();
//end add
...
//add
if ($gridSpan > 1) {
$objWriter->startElement('w:gridSpan');
$objWriter->writeAttribute('w:val',$gridSpan);
$objWriter->endElement();
//end add
...
}
}
}
}

你可以这样使用它:

$PHPWord  = new PHPWord();
$section = $PHPWord->createSection();
$table = $section->addTable();
$table->addRow();
$table->addCell(1000,null,2)->addText('info');

$table->addRow();
$table->addCell(500)->addText('Name');
$table->addCell(500)->addText('Sex');

对不起样式,你可以这样使用:

$tableStyle = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$PHPWord->addTableStyle('tableStyle',$tableStyle,null);
$table = $section->addTable('tableStyle');
$table->addRow();
$table->addCell(1000,null,2)->addText('info',null,array('align'=>'center'));

$table->addRow();
$table->addCell(500)->addText('Name',null,array('align'=>'center'));
$table->addCell(500)->addText('Sex',null,array('align'=>'center'));

关于phpword - 如何在 PHPWord 中合并单元格?或者在每个表格行中有不同数量的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925337/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com