【问题标题】:PHPExcel error in CodeIgniter "Unable to load the requested class: iofactory"CodeIgniter 中的 PHPExcel 错误“无法加载请求的类:iofactory”
【发布时间】:2013-02-22 15:59:39
【问题描述】:

我正在尝试使用 PHPExcel 1.7.8 + CodeIgniter 2.1.3 导出 XLS 文件

我已按照PHPExcel 的所有指示进行操作

但我收到此错误:

无法加载请求的类:iofactory

这是我的控制器代码:

    //expoxt to excel all admin data
function export_excel_admin()
{
    //$data['resultsadmin'] = $this->admin_model->get_all_data_admin();
    //var_dump($data['resultsadmin']);
    //$this->load->view('administrator/export_excel/export_excel_admin', $data);
    $query = $this->db->get('tbl_admin');

    if(!$query)
        return false;

    // Starting the PHPExcel library
    $this->load->library('excel');
    $this->load->library('PHPexcel/IOFactory');

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");

    $objPHPExcel->setActiveSheetIndex(0);

    // Field names in the first row
    $fields = $query->list_fields();
    $col = 0;
    foreach ($fields as $field)
    {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
        $col++;
    }

    // Fetching the table data
    $row = 2;
    foreach($query->result() as $data)
    {
        $col = 0;
        foreach ($fields as $field)
        {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
            $col++;
        }

        $row++;
    }

    $objPHPExcel->setActiveSheetIndex(0);

    $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

    // Sending headers to force the user to download the file
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
    header('Cache-Control: max-age=0');

    $objWriter->save('php://output');

}

啊,我也删除了 IOFactory.php 文件中的“PHPExcel_”部分 这个问题的任何解决方案?

【问题讨论】:

  • 您是否也修改了 PHPExcel 自动加载器?这样它就知道 IOFactory 实际上是 PHPExcel 的一部分?就我个人而言,我不会遵循该建议来更改 PHPExcel 类名。但将其设置为在供应商空间中正确使用自动加载器
  • @MarkBaker:不,我刚刚删除了 IOFactory.php 中的“PHPExcel_”部分,但实际上即使我不删除“PHPExcel_”部分,问题仍然存在..
  • 我发现很难提供帮助...我不在任何框架中使用 PHPExcel,但 PHPExcel 自动加载器应该作为完全符合 SPL 的自动加载器与任何框架一起工作,无需更改类名或任何方式的 PHPExcel 代码。 ahowto.net/php/… 可能会提供将 PHPExcel 集成到 Codeigniter 的替代方法

标签: php codeigniter phpexcel


【解决方案1】:

我之前已经成功使用 PHPExcel 和 CodeIgniter。

我所做的只是将 phpexcel 文件夹放入 application/third-party 并创建以下包装库:

<?php

class Excel {

    private $excel;

    public function __construct() {
        // initialise the reference to the codeigniter instance
        require_once APPPATH.'third_party/phpexcel/PHPExcel.php';
        $this->excel = new PHPExcel();    
    }

    public function load($path) {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        $this->excel = $objReader->load($path);
    }

    public function save($path) {
        // Write out as the new file
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save($path);
    }

    public function stream($filename) {       
        header('Content-type: application/ms-excel');
        header("Content-Disposition: attachment; filename=\"".$filename."\""); 
        header("Cache-control: private");        
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save('php://output');    
    }

    public function  __call($name, $arguments) {  
        // make sure our child object has this method  
        if(method_exists($this->excel, $name)) {  
            // forward the call to our child object  
            return call_user_func_array(array($this->excel, $name), $arguments);  
        }  
        return null;  
    }  
}

?>

然后我可以在我的控制器中执行以下操作:

$this->load->library("excel");
$this->excel->load("/path/to/input.xls");
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->SetCellValue('B2', "whatever");
$this->excel->save("/path/to/output.xls");

希望这对您有所帮助?

【讨论】:

  • 您是否更改了 PHPExcel 类中的任何内容?
  • 不是香肠.. 直接从 zip 文件中放入
  • 我的 excel 文件在我在 windows 机器上测试时下载,但在启用 https 的 linux 机器上部署时失败
  • hi.你能帮我导出到excel吗
  • @devrooms 正如 Dipen 所说的任何解决方案。我也面临同样的问题(ubuntu ec2 with https)
【解决方案2】:

也许这个评论太晚了。但也许对其他人有用。

更改以下部分代码:

// Starting the PHPExcel library
$this->load->library('excel');
$this->load->library('PHPexcel/IOFactory');

到:

// Starting the PHPExcel library
$this->load->library('excel');
//$this->load->library('PHPexcel/IOFactory');

那就把这部分代码也改一下吧:

 $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

到:

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

【讨论】:

    【解决方案3】:

    你只需要

    //afrer this you can use any of PHPExcel classes and methods
    $this->load->file(APPPATH.'libraries/PHPExcel.php'); //full path to 
    
    $objReader = new PHPExcel_Reader_Excel2007();//change by filetype
    try {
        $objPHPExcel = $objReader->load($inputFileName); //you file name
    } catch (Exception $e) {
        show_error($e->getMessage());
    }
    

    在 CI 2.1.3 && PHPExcel 1.7.8 上测试

    【讨论】:

      【解决方案4】:

      我按照这些说明进行 CodeIgniter 和 PHPExcel 之间的集成。

      在 CI 的 application/libraries/ 中创建一个新的 PHP 文件并将其命名为 Excel.php。

      require_once APPPATH."/third_party/PHPExcel.php";
      class Excel extends PHPExcel {
       public function __construct(){
        parent::__construct(); 
       } 
      }
      

      要遵循完整的说明,请访问Easily Integrateload Phpexcel Into Codeigniter Framework

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 2011-12-23
        • 2013-05-14
        • 1970-01-01
        • 1970-01-01
        • 2015-05-18
        • 1970-01-01
        相关资源
        最近更新 更多