首页 > 解决方案 > 在 phpword 中调用未定义的方法 Phpword::addSection()

问题描述

我最近开始在我的 Codeigniter 项目中使用 PHPWord 库。当我尝试使用 Stackoverflow 的建议之一创建表时 - 我收到致命错误:调用未定义的方法 Phpword::addSection() 。

以下是我来自 Controller 的代码:

        $this->load->library('Phpword');
$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}");
            }
        }

// 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('C:\xampp\htdocs\practice_ci\CodeIgniter\template.docx');

// Replace mark by xml code of table
        $template_document->setValue('table', $tablexml);

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

标签: phpcodeigniterphpword

解决方案


推荐阅读