CodeIgniter Forums

Full Version: Creating an Excel download
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In my app I have a function to create an  excel download from the database. I do this using a controller, a helper and a model

Here is my controller, Excel.php

PHP Code:
   public function export_to_excel()
       $export create_excel_export();

       $filename $export['filename'];
       $headers $export['headers'];
       $data $export['data'];
       header("Content-disposition: attachment; filename=".$filename." ".date("Y-m-d").".xls");
       header("Content-Type: application/");

       print "$headers\n$data";

Here is my helper, excel_helper.php

PHP Code:
   function create_excel_export()
       $ci = & get_instance();
$users = $ci->users_model->find_users();

$headers = array("Employee Name", "Employee Email", "Department");

$title = "";
$data = "";
$filename = 'Users';
$headers as $value) {
$title .= $value . "\t";
$headers = trim($title). "\n";

        if (!empty(
            foreach (
$users as $row){
$line = '';
$employee_name = $row['full_name'];
$email = $row['email'];
$dept_name = $row['dept_name'];

$line .= $employee_name . "\t";
$line .= $email . "\t";
$line .= $dept_name . "\t";

$data .= trim($line). "\n";

$export = array(
            'filename' => 
            'headers' => 
            'data' => 


Here is my model, Users_model.php

PHP Code:
   public function find_users(){
"SELECT * FROM users";

       $query $this->db->query($str);
       $result $query->result();

       $users = array();
       foreach ($result as $r){

           $user = array(
               'full_name'     => $r->full_name,
               'email'         => $r->email,
               'dept_name'     => $r->dept_name

               $users[] = $user;


       return $users;


While running this does cause an excel file to be downloaded as expected - the browser console report an error - 'Failed to load resource: Frame load interrupted'

I have modified my controller by adding this but it makes no difference

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/x-unknown");
header("Content-Disposition: attachment; filename='theFilename.ext'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: 200000");

Has anyone ever seen anything like this? I am using Safari on Mac OSX
Try setting this and see if it works.

header("Content-Length: 177998"); // you might want to set this
Tried this and no joy unfortunately. It appears to be a Safari issue as it works on other browsers.

This issue is widely reported on Google but no one has a solution for it. :-(
i'm really new to how this all works. I can read PHP to just a basic level but I heard using codeigniter will help me develop my apps faster, using your code I edited it to the fields in my database. I am lost on how to get the code to execute?
regular PHP you can create a button and link it to the PHP file, I tried that on CI using

<button type="button" class="btn btn-success"
onclick="<?php echo base_url()?>controller/excel">Export</button>

but the button doesn't do anything. Please Help
The html onclick is for executing a javascript function.
(06-07-2016, 12:48 PM)InsiteFX Wrote: [ -> ]The html onclick is for executing a javascript function.

Thanks for the clarification, but I also tried using a form with the action pointing to the excel.php file but still having no luck.

Sorry for being such an amateur!
Instead of the onclick event use a anchor <a> tag or CI anchor url_helper and set the ref to your controller and method.
If you are just trying to create a spreadsheet download, consider PHP's built in functions:


$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields);


I make quite a few CSV files using code similar to this.