How to download excel,csv file in Codeigniter

First Step:- Download this excel library here : click here

Next, we need to download phpExcel library from this link, and extract into application/third_party folder. After extract this library move to PHPExcel folder like application/third_party/PHPExcel and also move PHPExcel.php file to application/third_party/PHPExcel.php

Now we need to create Excel.php file into application/library, So go to application/library and create one file name Excel.php and put the below code here.

if (!defined('BASEPATH')) exit('No direct script access allowed');  
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel {
    public function __construct() {

Create Database With Table

you can use the below sql query for creating a table in your database.

    id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    first_name varchar(100) NOT NULL COMMENT 'First Name',
    last_name varchar(100) NOT NULL COMMENT 'Last Name',
    email varchar(255) NOT NULL COMMENT 'Email Address',
    dob varchar(20) NOT NULL COMMENT 'Date of Birth',
    contact_no varchar(50) NOT NULL COMMENT 'Contact No',
    PRIMARY KEY (id)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=1;
  INSERT INTO import (id, first_name, last_name, email, dob, contact_no) VALUES
  (1, 'Team', 'test', '', '21-02-2011', '9000000001'),
  (2, 'Admin', 'second', '', '21-02-2011', '9000000002'),
  (3, 'User', 'third', '', '21-02-2011', '9000000003'),
  (4, 'Editor', 'fouth', '', '21-02-2011', '9000000004'),
  (5, 'Writer', 'fifth', '', '21-02-2011', '9000000005'),
  (6, 'Contact', 'sixth', '', '21-02-2011', '9000000006'),
  (7, 'Manager', 'seven', '', '21-02-2011', '9000000007');

Now we need to create a controller name Export.php. In this controller we will create some method/function. We will build some of the methods like :

 defined('BASEPATH') OR exit('No direct script access allowed');
class Export extends CI_Controller {
    // construct
    public function __construct() {
        // load model
        $this->load->model('Export_model', 'export');

public function list_of_grievance_excel_download()

    $fileName = 'data-' . time() . '.xlsx';
    $empInfo = $this->export->getallgrievanceData_excel();
      $objPHPExcel = new PHPExcel();
    // set Header
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Name of Person/StartUp/Incubators');
    $objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Query Type');
    $objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Email');
    $objPHPExcel->getActiveSheet()->SetCellValue('D1', 'Contact_No');
    $objPHPExcel->getActiveSheet()->SetCellValue('E1', 'Query Type');
    $objPHPExcel->getActiveSheet()->SetCellValue('F1', 'Query/Grievance Description');
    $objPHPExcel->getActiveSheet()->SetCellValue('G1', 'Query Date');
    $objPHPExcel->getActiveSheet()->SetCellValue('H1', 'Status');
    $objPHPExcel->getActiveSheet()->SetCellValue('I1', 'Reply Comments');
    $objPHPExcel->getActiveSheet()->SetCellValue('J1', 'Reply Date');
    // set Row
    $rowCount = 2;
    foreach ($empInfo as $element) {
      $objPHPExcel->getActiveSheet()->SetCellValue('A' . $rowCount, $element['first_name'].' '.$element['last_name']);
      $objPHPExcel->getActiveSheet()->SetCellValue('B' . $rowCount, $element['post_type']);
      $objPHPExcel->getActiveSheet()->SetCellValue('C' . $rowCount, $element['email_id']);
      $objPHPExcel->getActiveSheet()->SetCellValue('D' . $rowCount, $element['mobile']);
      $objPHPExcel->getActiveSheet()->SetCellValue('E' . $rowCount, $element['query']);
      $objPHPExcel->getActiveSheet()->SetCellValue('F' . $rowCount, $element['comments']);
      $objPHPExcel->getActiveSheet()->SetCellValue('G' . $rowCount, date("d-m-Y",$element['added_on']));

      if (($element['is_closed']==0)) {
       if (!empty($element['user_id'])) {
           $status = "Replied";
            } else {
           if ($element['is_reply'] == 0) { 
            $status= 'Pending';
            } else { 
              $status= 'Replied';
        $status = 'Closed';        
       $objPHPExcel->getActiveSheet()->SetCellValue('H' . $rowCount, $status);
        $objPHPExcel->getActiveSheet()->SetCellValue('I' . $rowCount, $element['reply_comments']);
        $objPHPExcel->getActiveSheet()->SetCellValue('J' . $rowCount, date('d-m-Y',strtotime($element['reply_date'])));
    $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
    $file_path . 'assets/Grievance/';
    $objWriter->save(file_path . $fileName);
    // download file
    header("Content-Type: application/");
    redirect(file_path . $fileName);

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button