PHPExcel error in CodeIgniter "Unable to load the requested class: iofactory"

26,974

Solution 1

I've used PHPExcel with CodeIgniter successfully before.

All I did was drop the phpexcel folder into application/third-party and created the following wrapper library:

<?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;  
    }  
}

?>

I could then do the following in my controllers:

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

Hope this helps you out?

Solution 2

Maybe this comment was too late. But maybe useful for others.

Change the part code below:

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

to:

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

Then change this part code too:

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

to:

 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
Share:
26,974
sheldon90
Author by

sheldon90

just ordinary person, interested in image processing..

Updated on September 28, 2020

Comments

  • sheldon90
    sheldon90 over 3 years

    I'm trying to export an XLS file with PHPExcel 1.7.8 + CodeIgniter 2.1.3

    I have followed all the instructions from PHPExcel

    but I'm getting this error:

    Unable to load the requested class: iofactory

    and here's my Controller code:

        //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');
    
    }
    

    Ah also I have removed the "PHPExcel_" part in IOFactory.php file any solution for this issue?

  • sheldon90
    sheldon90 about 11 years
    did you change anything in the PHPExcel Class?
  • devrooms
    devrooms about 11 years
    Not a sausage.. Dropped it in straight from the zip file
  • Dipen
    Dipen about 9 years
    My excel file is downloaded on when i test on windows machine but fails when deployed on linux machine with https enabled
  • robins
    robins almost 8 years
    hi.can u help me about export to excel
  • Deep 3015
    Deep 3015 almost 7 years
    @devrooms as Dipen says any solution. I am also facing same problem (ubuntu ec2 with https)
  • Deep 3015
    Deep 3015 almost 7 years
    @Dipen have you got solution