【发布时间】:2020-04-24 20:34:10
【问题描述】:
我正在尝试在 Word 文档中使用块,但遇到了一些问题。首先,当我在文档中声明一个块时,如果我不使用“cloneBlock”函数,结果如下所示:
${sec}
example
${/sec}
也许我必须使用该功能才能正常显示。但我的主要问题是“deleteBlock”不起作用。如果我不克隆该块,则生成的 docx 已损坏。但是,如果我克隆该块,“deleteBlock”函数不会删除该块,它会在我的最终 docx 文件中显示该块内的信息。
这是我的代码:
//Word
// Creating the new document...
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('../example.docx');
//set value
//$templateProcessor->setValue('title', 'Example');
//Triplicate block
$templateProcessor->cloneBlock('firstblock', 3, true, true);
$templateProcessor->setValue('firstname#1', 'John');
$templateProcessor->setValue('lastname#1', 'Doe');
$templateProcessor->setValue('firstname#2', 'John');
$templateProcessor->setValue('lastname#2', 'Doe');
$templateProcessor->setValue('firstname#3', 'John');
$templateProcessor->setValue('lastname#3', 'Doe');
//Delete Block
$templateProcessor->cloneBlock('sec', 1, true, true);
$templateProcessor->deleteBlock('sec');
$templateProcessor->saveAs('example.docx');
Docx 模板:
${firstblock}
Hello ${firstname} ${lastname}!
${/firstblock}
${sec}
example
${/sec}
更新: 我没有使用函数“deleteBlock”,而是使用了这样的函数“cloneBlock”,它删除了块:
//Delete Block
$templateProcessor->cloneBlock('sec', 0, true, true);
所以,我写了 0 次克隆块,所以它消失了 但我还有另一个问题。我不知道为什么,但这只是有时有效
【问题讨论】:
-
您找到解决方案了吗?目前我最终创建了多个模板文件,因为我无法删除块或行