We had a need to export database data from a web application to excel we were developing using the codeignitor framework. The worst thing that frustrates me with tutorials online there is always an assumption of the projects settings and these are omitted I will try and explain everything I have installed.
 
Project Assumptions (These are part of the core and using in the project).

  1. Codeigniter Base Model – My CodeIgniter Base Model is an extended CI_Model class to use in your CodeIgniter applications. It provides a full CRUD base to make developing database interactions easier and quicker, as well as an event-based observer system, in-model data validation, intelligent table name guessing and soft delete.
  2. I also extended the CI_Controller and Called it Front_controller()
  3. Codeigniter Version – 2.1.4
  4. Download a copy of PHPExcel from Github

 
File and folder Instructions

  1. Create a new folder export in your public_html folder or the www folder on your server.
  2. Extract the contents of the PHPExcel file you downloaded or cloned and copy the contents of the classes directory into application/third_Party folder, the third_party folder should have a folder called PHPExcel and a file called PHPExcel.php.
  3. Create a new file in application/libraries called excel.php
  4. Create a new file in application/controller called export.php (or anyname that you like)

 
Code for the file excel.php in application/libraries

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Excel {

    private $excel;

    public function __construct() {
        require_once APPPATH . 'third_party/PHPExcel.php';
        $this->excel = new PHPExcel();
    }

    public function load($path) {
        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        $this->excel = $objReader->load($path);
    }

    public function save($path) {
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        $objWriter->save($path);
    }

    public function stream($filename, $data = null) {
        if ($data != null) {
            $col = 'A';
            foreach ($data[0] as $key => $val) {
                $objRichText = new PHPExcel_RichText();
                $objPayable = $objRichText->createTextRun(str_replace("_", " ", $key));
                $this->excel->getActiveSheet()->getCell($col . '1')->setValue($objRichText);
                $col++;
            }
            $rowNumber = 2;
            foreach ($data as $row) {
                $col = 'A';
                foreach ($row as $cell) {
                    $this->excel->getActiveSheet()->setCellValue($col . $rowNumber, $cell);
                    $col++;
                }
                $rowNumber++;
            }
        }
        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("export/$filename");
        header("location: " . base_url() . "export/$filename");
        unlink(base_url() . "export/$filename");
    }

    public function __call($name, $arguments) {
        if (method_exists($this->excel, $name)) {
            return call_user_func_array(array($this->excel, $name), $arguments);
        }
        return null;
    }
}

Code for the file export.php in application/controller

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Export extends Front_Controller {

    public function __construct() {
        parent::__construct();
        // Load the Library
        $this->load->library("excel");
        // Load the Model
        $this->load->model("Your_model_name");
    }

    public function index() {
        $this->excel->setActiveSheetIndex(0);
        // Gets all the data using MY_Model.php
        $data = $this->Your_model_name->get_all();

        $this->excel->stream('name_of_file.xls', $data);
    }

}

I hope this helps someone when they want to export data to Excel with codeigniter and PHPExcel.