表格后的PhpWord中的addText

问题描述

我正在使用下面的代码用表格代替标记。 桌子后,我尝试添加文本

$section->addText("That was the table");

...但是文本不会显示,只会显示表格。

如何编辑文本也显示在文档中的代码

//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}");
    }
}
$section->addText("That was the table");

// Create writer to convert document to xml
$objWriter = \PHPOffice\PHPWord\IOFactory::createWriter($document_with_table,'Word2007');

// Get all document xml code
$fullxml = $objWriter->getWriterPart('Document')->write();

// Get only table xml code
$tablexml = preg_replace('/^[\s\S]*(<w:tbl\b.*<\/w:tbl>).*/','$1',$fullxml);

//Open template with ${table}
$template_document = new \PHPOffice\PHPWord\TemplateProcessor('template.docx');

// Replace mark by xml code of table
$template_document->setValue('table','</w:t></w:r></w:p>'.$tablexml.'<w:p><w:r><w:t>');

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)