首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPExcel错误在CodeIgniter中“无法加载所请求的类: iofactory”

问PHPExcel错误在CodeIgniter中“无法加载所请求的类: iofactory”
EN

Stack Overflow用户
提问于 2013-02-22 15:59:39
回答 4查看 27.2K关注 0票数 2

我试图用PHPExcel 1.7.8 + CodeIgniter 2.1.3导出XLS文件

我遵守了PHPExcel的所有指示

但我发现了一个错误:

无法加载请求的类:iofactory

这是我的控制器代码:

代码语言:javascript
复制
    //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文件中的“IOFactory.php”部分,解决了这个问题吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-02-22 17:21:19

我以前成功地将PHPExcel与CodeIgniter结合使用过。

我所做的就是将phpexcel文件夹放到应用程序/第三方中,并创建了以下包装库:

代码语言:javascript
复制
<?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;  
    }  
}

?>

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

代码语言:javascript
复制
$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");

希望这能帮到你?

票数 8
EN

Stack Overflow用户

发布于 2015-02-05 04:36:51

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

更改以下部件代码:

代码语言:javascript
复制
// Starting the PHPExcel library
$this->load->library('excel');
$this->load->library('PHPexcel/IOFactory');

至:

代码语言:javascript
复制
// Starting the PHPExcel library
$this->load->library('excel');
//$this->load->library('PHPexcel/IOFactory');

然后也更改此部分代码:

代码语言:javascript
复制
 $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');

至:

代码语言:javascript
复制
 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
票数 3
EN

Stack Overflow用户

发布于 2013-02-28 00:56:06

你所需要的就是

代码语言:javascript
复制
//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上进行测试

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15028175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档