【发布时间】:2021-10-31 08:40:11
【问题描述】:
我正在使用 PHPWord 创建报告 make use of a .docx template。
这个库允许我clone a block 和clone table rows。但是,这些示例彼此分开。我需要结合这两种方法,因为我的模板表如下所示:
${block_group}
+--------+----------------------------------------------------------------+
| Group: | ${group} |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
${/block_group}
要求:
- 此报告将为每个
group- (clone block) 提供一个单独的表格 - 每个
group/block可以有多个表行-(clone table rows)
我有什么
到目前为止,这是我的代码的样子:
# Create the template processor
$templateProcessor = new TemplateProcessor('/path/to/template/Template.docx');
# Block cloning
$replacements = array(
array('group' => 'Group 1'),
array('group' => 'Group 2')
);
$templateProcessor->cloneBlock('block_group', 0, true, false, $replacements);
如您所见,$replacements 只处理了${group} 占位符,因为这是我对clone block 步骤唯一担心的事情。我现在有两张表,${group} 占位符设置正确,${name} 和 ${address} 占位符设置正确。
现在我需要为每个组克隆表行,我被卡住了,我什至不知道如何开始编码。
这是我运行当前代码时得到的文件的样子:
+--------+----------------------------------------------------------------+
| Group: | Group 1 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
+--------+----------------------------------------------------------------+
| Group: | Group 2 |
+--------+------------+---------------------------------------------------+
| Name | Address |
+---------------------+---------------------------------------------------+
| ${name} | ${address} |
+---------------------+---------------------------------------------------+
【问题讨论】:
标签: php templates ms-word phpword