在phpWord模板文档中插入表



我有一个template.docx模板,里面有标记${table}。我需要使用phpWord创建表,并将其插入我的template.docx而不是内部的${table}标记中。这是我的代码示例

//Create simple table
$document_with_table = new PhpWord();
$section = $document_with_table->addSection();
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
$table->addRow();
for ($c = 1; $c <= 5; $c++) {
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
}
}
//Open template with ${table}
$template_document = new PhpOfficePhpWordTemplateProcessor('template.docx');
// some code to replace ${table} with table from $document_with_table
// ???

//save template with table
$template_document->saveAs('template_with_table.docx');

首先,我使用新的PhpWord实例在单独的变量$document_with_table中创建表。接下来,我将template.docx加载到$template_document变量中。现在我需要将表从$document_with_table插入到$template_document,而不是在里面插入${table}标记。我该怎么做?

PhpWord版本-最新稳定版(0.16.0)

PhpWord有另一个解决方案,对我来说更好。

use PhpOfficePhpWordElementTable;
use PhpOfficePhpWordTemplateProcessor;
$table = new Table(array('unit' => TblWidth::TWIP));
foreach ($details as $detail) {
$table->addRow();
$table->addCell(700)->addText($detail->column);
$table->addCell(500)->addText(1);
}
$phpWord = new TemplateProcessor('template.docx');
$phpWord->setComplexBlock('{table}', $table);
$phpWord->saveAs('template_with_table.docx');

您可以获取表的xml代码并将其插入到模板中

//Create table
$document_with_table = new PhpWord();
$section = $document_with_table->addSection();
$table = $section->addTable();
for ($r = 1; $r <= 8; $r++) {
$table->addRow();
for ($c = 1; $c <= 5; $c++) {
$table->addCell(1750)->addText("Row {$r}, Cell {$c}");
}
}
// Create writer to convert document to xml
$objWriter = PhpOfficePhpWordIOFactory::createWriter($document_with_table, 'Word2007');
// Get all document xml code
$fullxml = $objWriter->getWriterPart('Document')->write();
// Get only table xml code
$tablexml = preg_replace('/^[sS]*(<w:tblb.*</w:tbl>).*/', '$1', $fullxml);
//Open template with ${table}
$template_document = new PhpOfficePhpWordTemplateProcessor('template.docx');
// Replace mark by xml code of table
$template_document->setValue('table', $tablexml);
//save template with table
$template_document->saveAs('template_with_table.docx');

问题是您没有创建表函数的权限。

我只是使用了以下代码。显然,您需要表中的参数:${serial_numbers_table}。我只是在保存一个与.xml表类似的word文档时随机提取了属性。

// First add table and its properties like grid column
$snums_tbl = 
'<w:tbl><w:tblPr><w:tblStyle w:val="Grilledutableau"/>' 
. '<w:tblW w:w="0" w:type="auto"/>'
. '<w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0"'
. ' w:noHBand="0" w:noVBand="1"/></w:tblPr><w:tblGrid>'
. '<w:gridCol w:w="4675"/><w:gridCol w:w="4675"/></w:tblGrid>' ; 
// Header row
$snums_tbl .= 
'<w:tr>'
. '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
. '<w:p><w:t>Equipment Serial Number</w:t></w:p></w:tc>'
. '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
. '<w:p><w:t>Applied Attesta Label Number</w:t></w:p></w:tc>'
. '</w:tr>' ;
foreach ($serial_nums as $serial_num)
{
$snums_tbl .= 
'<w:tr>'
. '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
. '<w:p><w:t>' . $serial_num -> serialNumber . '</w:t></w:p></w:tc>'
. '<w:tc><w:tcPr><w:tcW w:w="4675" w:type="dxa"/></w:tcPr>'
. '<w:p><w:t>' . $serial_num -> LabelNumber . '</w:t></w:p></w:tc>'
. '</w:tr>' ;
} 
$snums_tbl .= '</w:tbl>' ;
$tmpl -> setValue("serial_numbers_table",$snums_tbl) ;

最新更新