PHP CodeIgniter export excel

//in a library file export.php
if (!defined('BASEPATH')) exit('No direct script access allowed');

* Excel library for Code Igniter applications
* Based on: Derek Allard, Dark Horse Consulting,, April 2006
* Tweaked by: Moving.Paper June 2013

class Excel{
 function to_excel($array, $filename) {
 header('Content-type: application/');
 header('Content-Disposition: attachment; filename='.$filename.'.xls');

 //Filter all keys, they'll be table headers
 $h = array();
 foreach($array->result_array() as $row){
 foreach($row as $key=>$val){
 if(!in_array($key, $h)){
 $h[] = $key; 
 //echo the entire table headers
 echo '<table><tr>';
 foreach($h as $key) {
 $key = ucwords($key);
 echo '<th>'.$key.'</th>';
 echo '</tr>';
 foreach($array->result_array() as $row){
 echo '<tr>';
 foreach($row as $val)
 echo '</tr>';
 echo '</table>';
 function writeRow($val) {
 echo '<td>'.utf8_decode($val).'</td>'; 


You may also like...

Leave a Reply

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