类 'PhpOffice\PhpWord\Writer\Word2007\Element\Section'未找到

发布于 2025-01-13 19:21:44 字数 622 浏览 3 评论 0原文

我正在尝试使用以下代码将列表项添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2025-01-20 19:21:45

库开发人员已经在研究这个问题,但解决方案仍在 PR 上: https://github.com /PHPOffice/PHPWord/pull/2562

其实解决办法很简单,只需要添加一个if,然后将Section elementName替换为Container即可。就我而言,我使用的是 TemplateProccessor 类,因此我只需复制 PR 代码,创建自定义 TemplateProcessor 类并覆盖 setComplexBlock 函数,这是一个临时解决方案,直到 PR 合并之后,您应该避免直接修改库代码。

use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\TemplateProcessor as PHPWordTemplateProcessor;

 class TemplateProcessor extends PHPWordTemplateProcessor
 {

   public function setComplexBlock($search, AbstractElement $complexType): void
   {
    $elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
    
    //This is the solution
    if ($elementName === 'Section') {
        $elementName = 'Container';
    }
    $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;

    $xmlWriter = new XMLWriter();
    /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter */
    $elementWriter = new $objectClass($xmlWriter, $complexType, false);
    $elementWriter->write();

    $this->replaceXmlBlock($search, $xmlWriter->getData(), 'w:p');
 }
}

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.

use PhpOffice\PhpWord\Element\AbstractElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\TemplateProcessor as PHPWordTemplateProcessor;

 class TemplateProcessor extends PHPWordTemplateProcessor
 {

   public function setComplexBlock($search, AbstractElement $complexType): void
   {
    $elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
    
    //This is the solution
    if ($elementName === 'Section') {
        $elementName = 'Container';
    }
    $objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;

    $xmlWriter = new XMLWriter();
    /** @var \PhpOffice\PhpWord\Writer\Word2007\Element\AbstractElement $elementWriter */
    $elementWriter = new $objectClass($xmlWriter, $complexType, false);
    $elementWriter->write();

    $this->replaceXmlBlock($search, $xmlWriter->getData(), 'w:p');
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文