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>
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
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…
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=sport_report.xls");
header("Pragma: no-cache");
header("Expires: 0");
what about the queries to get data from database insert to that excel.?
please your advise…
Just made it… http://avenir.ro/codeigniter-retrieve-data-from-database/ Hope you will like it.
I’ve tried this tutorial and success
and i need some advice if i want to add a formula with this method…
thanks..
Hello. For what you need there is a more complex plugin you can use. Its name is PHPExcel. EllisLab, the ones who made Codeigniter, integrated PHPExcel as a plugin, and that plugin can be found on github: https://github.com/EllisLab/CodeIgniter/wiki/PHPExcel. I hope that info will be of some help to you.
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! 🙂
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.
I like it, it’s simple, like btcExcel with jquery plugin, but why i cant get value from post submit?
thanks.
I don’t understand your question.
[…] Codeigniter: Export data into an Excel file – I will show you how to export data into an excel file by using Codeigniter framework. This tutorial won’t teach you how to use controllers, views or models…. […]
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?
thenx also give data export into pdf file
thanks!!