php - 在 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');
解决方案
推荐阅读
- bash - 为什么我的文件不是来自 bash 脚本?
- javascript - 显示所有标签内容 vanilla js
- java - Java 亲和锁
- c - 允许 ((int *)p)++ 的任何 gcc 选项
- grails - 为什么我不能在 Netbeans 8.2 中创建新的 grails 应用程序?
- java - 强制实现枚举的 Java 接口 - 如何处理?
- r - 在 R 中编写多个 .txt
- javascript - 使用带有按钮类型的 jquery ajax 提交表单而不是提交类型
- php - 更改php数组格式
- node.js - 无法安装 appium - npm ERR!代码 EAI_AGAIN