类 'PhpOffice\PhpWord\Writer\Word2007\Element\Section'未找到
我正在尝试使用以下代码将列表项添加到 phpword 模板:
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(storage_path('app/template_report.docx'));
$section = $PHPWord->addSection();
$section->addListItem('List Item 1', 0);
$section->addTextBreak(1);
$document->setComplexBlock('list_items', $section);
$save_file_name = storage_path('app/report_'.date('Ymd_His').'.docx');
$document->saveAs($save_file_name);
但返回以下错误:
未找到“PhpOffice\PhpWord\Writer\Word2007\Element\Section”类
为什么会发生以及如何解决?
谢谢
I am trying to add list item to phpword template with following code:
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(storage_path('app/template_report.docx'));
$section = $PHPWord->addSection();
$section->addListItem('List Item 1', 0);
$section->addTextBreak(1);
$document->setComplexBlock('list_items', $section);
$save_file_name = storage_path('app/report_'.date('Ymd_His').'.docx');
$document->saveAs($save_file_name);
but return following error:
Class 'PhpOffice\PhpWord\Writer\Word2007\Element\Section' not found
Why its happend and how to solve it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
库开发人员已经在研究这个问题,但解决方案仍在 PR 上: https://github.com /PHPOffice/PHPWord/pull/2562。
其实解决办法很简单,只需要添加一个if,然后将Section elementName替换为Container即可。就我而言,我使用的是 TemplateProccessor 类,因此我只需复制 PR 代码,创建自定义 TemplateProcessor 类并覆盖 setComplexBlock 函数,这是一个临时解决方案,直到 PR 合并之后,您应该避免直接修改库代码。
Library Developers already work on this, but the solution it's still on PR: https://github.com/PHPOffice/PHPWord/pull/2562.
The solution is pretty simple actually, you only need to add an if and replace the Section elementName to Container. In my case, i'm using TemplateProccessor class so i just copy the PR code, create my custom TemplateProcessor class and override the setComplexBlock function, this is a temporary solution until the PR merge after that you should avoid modify directly the library code.