CodeIgniter: How to export data to Excel file

I will assume that you know how to use CodeIgniter, know what controllers, models and views are and what they do. If you don’t know anything about these terms, maybe you really don’t need to find out about how you can export data to an Excel spreadsheet.

Let’s start by making the controller:

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Data extends CI_Controller
{
  public function index()
  {
    $this->load->view('data_page_view'); 
  }
  public function toExcel()
  {
    $this->load->view('spreadsheet_view');
  }
}

The view from which the visitors will be invited to export data in an excel spreadsheet (‘data_page_view.php‘) would have to have at least a link that allows the user to download the spreadsheet:

<a href='data/toExcel'>Export Data</a>

And the view which will actually make the spreadsheet (‘spreadsheet_view.php‘) would look like this:

<?php
// We change the headers of the page so that the browser will know what sort of file is dealing with. Also, we will tell the browser it has to treat the file as an attachment which cannot be cached.

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=exceldata.xls");
header("Pragma: no-cache");
header("Expires: 0");
?>
<table border='1'>
  <tr>
    <td>ID</td>
    <td>First Name</td>
    <td>Last Name</td>
    <td>Important info</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>Nothing really...</td>
  </tr>
</table>

 

15 comments

  1. i had problem in server side.
    i got this error

    A PHP Error was encountered
    Severity: Warning
    Message: Cannot modify header information – headers already sent by (output started at /home/admin/application/controllers/report/report.php:1)
    Filename: report/report_list.php
    Line Number: 2

    1. Could you please send me the first two lines of report_list.php? I think the problem with your script is that you sent something to the browser before setting the header…

      1. <?php
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=sport_report.xls");
        header("Pragma: no-cache");
        header("Expires: 0");

  2. I’ve tried this tutorial and success
    and i need some advice if i want to add a formula with this method…
    thanks..

  3. How can I encode this to UTF-8? I get strange characters when I want to use: á é í ó ú
    Other than that thank you so much, I’ve been looking for this everywhere! 🙂

    1. Well… Never thought of that, but at the headers lines (inside the spreadsheet_view.php) you could start by putting the following header:
      header(‘Content-Type: text/html; charset=UTF-8’);
      Please tell me if that worked.

  4. I followed the headers you mentioned in this article. When I tried to open, downloaded .xls formatted file, I’ve got the following message,
    “The file you are trying to open, ‘exceldata.xls’, is in different than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?”

    Then I was proceed with “Yes”. But there is nothing inside the document. Should I do anything additional?

Leave a Reply

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

No spam? * Time limit is exhausted. Please reload CAPTCHA.