Problem PHPExcel + CodeIgniter

Topics: Developer Forum, User Forum
Jan 18, 2009 at 5:39 PM
Anyone ever use CodeIgniter before ?
I'm trying to use PHPExcel library within my CI code and i found this strange behaviour.

I want to do this :

Create on-the-fly spreadsheet based on a template.
So i have to load a template, manipulate the content, and send it to the browser.

I have this file in my CI library :

excel.php

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

class Excel  
{
    public $workbook;
    
    function __construct()
    {
        // PHPExcel libraries have to be in your include path !
        require_once('PHPExcel.php');
        require_once('PHPExcel/IOFactory.php');
    }
    
    function load()
    {
        // Path to the template file
        $template_location = 'resources/template.xls';
    
        $xls_reader = PHPExcel_IOFactory::createReader('Excel5');
        $this->workbook = $xls_reader->load($template_location);
        
        var_dump($this->workbook); // Yea, successfully load the data
    }
    
    function send()
    {
        //So far so good, now let's create the writer
    
        $xls_writer = PHPExcel_IOFactory::createWriter($this->workbook, 'Excel5'); // Trouble maker
        // Stops here, no error message, output nothing
        
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Type: application/force-download');
        header('Content-Type: application/octet-stream');
        header('Content-Type: application/download');
        header("Content-Disposition: attachment;filename=from-template.xls");
        header('Content-Transfer-Encoding: binary');

        $xls_writer->save('php://output');    
    }
}

And i have this tester class in my controller :

excel_tester.php

<?php

class Excel_tester extends Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function test()
    {
        $this->load->library('excel');
        
        $this->excel->load();
        $this->excel->send();
    }
}

And here goes the problem :

The template was loaded successfully. I tried to var_dump'ing the workbook object, and it outputs correctly.
The code execution stops right when i call PHPExcel_IOFactory::createWriter(). No error messages displayed. It just stopped.

I tried to change the parameter,
so instead of "createWriter($this->workbook, 'Excel5')", i use "createWriter(new PHPExcel(), 'Excel5')".
No luck.

BUT, when i omit the template load code (that is, no createReader executed in my code), the "createWriter(new PHPExcel(), 'Excel5')" works like a charm, and it outputs the empty xls correcly.


Why is that, and how can i fix my problem here ?
Any helps will be appreciated. As far as i found, PHPExcel fits my needs the most, and i don't want to abandon this library.
Developer
Jan 19, 2009 at 12:45 AM
Double post:
http://www.codeplex.com/PHPExcel/Thread/View.aspx?ThreadId=44498