Skip to content

Simple Excel Plugin

Derek Jones edited this page Jul 5, 2012 · 8 revisions

Category:Plugin::Data Conversion Category:Contributions::Plugins::Files

This is a simple plugin to create excel output.

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

/*
* Excel library for Code Igniter applications
* Author: Federico Ramírez a.k.a fedekun a.k.a lenkun - Feb 2010
*/

function to_excel($array, $filename='out') {
    header('Content-type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename='.$filename.'.xls');
    
    // Filter all keys, they'll be table headers
    $h = array();
    foreach($array as $row)
        foreach($row as $key=>$val)
            if(!in_array($key, $h))
                $h[] = $key;
    
    echo '';
    foreach($has$key) {
        $key = ucwords($key);
        echo'';
    }
    echo'';
    
    foreach($arrayas$val)
        _writeRow($val, $h);
    
    echo'
'.$key.'
'
; } function _writeRow($row, $h, $isHeader=false) { echo ''; foreach($h as $r) { if($isHeader) echo ''.utf8_decode(@$row[$r]).''; else echo ''.utf8_decode(@$row[$r]).''; } echo ''; }

Example usage

to_excel($this->model->getUsersAsArray());

Clone this wiki locally