Saturday, January 29, 2022

[FIXED] Codeigniter PHPExcel Class 'PHPExcel_IOFactory' not found

Issue

I have followed this tutorial here

https://arjunphp.com/how-to-use-phpexcel-with-codeigniter/

and here

http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/

However, I still get the PHPExcel_IOFactory not found error. I haven't found much help in this area. Is there an alternative excel plugin or is there a way to solve this?

This is my controllers construct

public function __construct() {        
        parent::__construct();
        $this->load->library('Excel');
    }

This is my controllers upload function

$objReader= PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true); 
$objPHPExcel= PHPExcel_IOFactory::load($document_folder."/".$file_name.".".$file_extension);    
//        $this->response($objPHPExcel);
$objPHPExcel=$objReader->load($document_folder."/".$file_name.".".$file_extension); 
$totalrows=$objPHPExcel->setActiveSheetIndex(0)->getHighestRow();
$objWorksheet=$objPHPExcel->setActiveSheetIndex(0);  
for($i=2;$i<=$totalrows;$i++){ //2 is one row after the header rows
    $title= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();     
    $first_name= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();            
    $last_name= $objWorksheet->getCellByColumnAndRow(2,$i)->getValue(); 
    $date_of_birth= $objWorksheet->getCellByColumnAndRow(3,$i)->getValue(); 
    $email=$objWorksheet->getCellByColumnAndRow(4,$i)->getValue();
    $phone_number=$objWorksheet->getCellByColumnAndRow(5,$i)->getValue(); 
    $company=$objWorksheet->getCellByColumnAndRow(6,$i)->getValue(); 
    $input=array(
          'title'=>$title, 
          'first_name'=>$first_name,
          'last_name'=>$last_name,
          'date_of_birth'=>$date_of_birth,
          'email'=>$email,
          'phone_number'=>$phone_number,
          'company'=>$company,
     );

Solution

You have to remember that PHPExcel is just PHP, and so is CodeIgniter. You don't have to load PHPExcel through another library. If fact, it doesn't do anything for you. I put all of the PHPExcel files in /third_party/, and then do this:

require_once( APPPATH . 'third_party/PHPExcel-1.8/Classes/PHPExcel.php');
$objReader = PHPExcel_IOFactory::createReader('Excel2007');

Notice that in the example I am using v1.8

If you want to use $objReader in multiple methods, then just make it a class property. So $this->objReader instead.



Answered By - Brian Gottier

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.