Jer*_*emy 3 php codeigniter phpword
我正在尝试使用PHPWord并且难以使用它.
这是我在application/libraries/words.php中的代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PhpWord/Autoloader.php";
\PhpOffice\PhpWord\Autoloader::register(); \\NOT SURE IF THIS IS CORRECT
class Word extends PhpWord {
public function __construct() {
parent::__construct();
}
}
Run Code Online (Sandbox Code Playgroud)
基于PHPWord github,我必须像这样使用它:
Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.
require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); // WHERE SHOULD I PUT THIS?
Run Code Online (Sandbox Code Playgroud)
当我尝试从我的控制器加载库时:
$this->load->library('word');
Run Code Online (Sandbox Code Playgroud)
它给我的错误说:
Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7
Run Code Online (Sandbox Code Playgroud)
我试图扩展Autoloader类,但PHP仍然抱怨它找不到Autoloader.php.
当我做
file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php
Run Code Online (Sandbox Code Playgroud)
它返回1.
有谁知道如何解决这一问题?
来自俄罗斯的爱;)在CodeIgniter的库中使用这样的:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/libraries/PHPWord/src/PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();
class Word extends Autoloader {
}
?>
Run Code Online (Sandbox Code Playgroud)
在控制器中来自样本的代码:
$this->load->library('word');
/* remove ;)
#require_once 'PHPWord/src/PhpWord/Autoloader.php';
#PhpOffice\PhpWord\Autoloader::register();
*/
// Template processor instance creation
echo date('H:i:s') , ' Creating new TemplateProcessor instance...' ;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('text.docx');
$templateProcessor->setValue('sss','123');
echo date('H:i:s'), ' Saving the result document...';
$templateProcessor->saveAs('test1.docx');
Run Code Online (Sandbox Code Playgroud)
测试它的作品!