【问题标题】:CodeIgniter PHPWord using as third_partyCodeIgniter PHPWord 用作第三方
【发布时间】:2014-09-25 05:05:59
【问题描述】:

我正在尝试使用 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(); 
    } 
}

基于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?

当我尝试从我的控制器加载库时:

$this->load->library('word');

它给我的错误是:

Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7

我尝试扩展 Autoloader 类,但 PHP 仍然抱怨找不到 Autoloader.php。

当我这样做时

file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php

它返回 1。

有谁知道如何解决这个问题?

【问题讨论】:

  • 你只试过Autoloader::register();吗?
  • @kumar_v 它说找不到类“自动加载器”。我试过 file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") 它返回 1.
  • Autoloader.php 中有 Autoloader 类吗?
  • @kumar_v 是的,这是它的内容github.com/PHPOffice/PHPWord/blob/master/src/PhpWord/…
  • 你找到解决方案了吗@JeremyRIrawan??

标签: php codeigniter phpword


【解决方案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 {

    }

    ?>

在示例代码中的控制器中:

$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');

测试它有效!

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2017-10-28
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多