How to import and export excel sheet using PHPExcel as a library with CodeIgniter ?

PHPExcel is a pure PHP library for reading and writing spreadsheet files and CodeIgniter is one of the well known PHP MVC framework. Here i am gonna show you how to Integrate PHPEXcel library in CodeIgniter.
  1. first download PHPEXcel library here is Link
  2. extract all file and put into /your-ci/application/third_party/
  • create new file in  /your-ci/application/libraries/Excel_library.php [ this file user as normaly as  library ] 
  • pull this code in this file
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 *  =======================================
 *  Author     : Amir shahzad
 *  License    : Protected
 *  Email      : amir.shahzad.mcs@gmail.com
 *  =======================================
 */
require_once APPPATH."/third_party/PHPExcel.php"; 
 
class Excel_library extends PHPExcel {
    public function __construct() {
        parent::__construct();
    }
}
?>
create a new controller in  /your-ci/application/controllers/Excel.php here is controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Author: Amir shahzad
*     amir.shahzad.mcs@gmail.com
*
*/
class Excel extends CI_Controller {
 
 function __construct(){
        parent::__construct();
    }
 
 public function index(){
  
  $this->load->library('excel_library');  
  $objPHPExcel = new PHPExcel();
  
  
  header('Content-type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="fileName.xlsx"');
  header('Cache-Control: max-age=0');
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  $objWriter->save('php://output');
 }
}

Share this

Related Posts

First