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.
Coordinator
Jan 18, 2009 at 6:09 PM
I've not tried working with CodeIgniter, but the example described at http://techxplorer.com/2008/12/08/using-phpexcel-with-codeigniter/ is similar to your code, but with one or two differences that might affect things
Jan 18, 2009 at 6:25 PM
Yea i start my code with that one :p . . techxplorer's code is working because he doesn't use createReader in his code.
The problem arise when i call createReader, followed by createWriter.
I don't see this problem when i code this in plain php (no CI).
Developer
Jan 19, 2009 at 12:07 AM
We cannot rule out that this is a bug in Excel5 writer. Maybe your template uses some advanced formulas that are not supported by Excel5 writer?

Can you try replacing your template.xls with a blank workbook and see if the script starts working?

If it is the template.xls that is the problem, you may send it to me so I can inspect (erik at phpexcel dot net).

Jan 19, 2009 at 12:53 AM
@koyama
template.xls is a fresh blank workbook. It is created using openoffice, but i don't think this is the problem.
I've tried to change createWriter's parameter, instead of "createWriter($this->workbook, 'Excel5')", i use "createWriter(new PHPExcel(), 'Excel5')
Nothing changed.
Developer
Jan 19, 2009 at 12:53 AM
Hmm... I now notice that you are saying that it stops before the save().. That probably rules out that it is a a bug in Excel5 writer. It must be something else...

Try to do a plain read/write with you template outside the CodeIgniter framework. (That is saving ordinarily to disk, not sending to browser.)

Jan 19, 2009 at 1:07 AM
It works correctly outside CI.

excel.php
<?php

class Excel
{
    private $workbook;

    function __construct()
    {
        // PHPExcel libraries have to be in your include path !        
        require_once('PHPExcel.php');
        require_once('PHPExcel/IOFactory.php');
    }
    
    function load()
    {
        // Location relative to the main caller
        $template_location = 'template.xls';
    
        $xls_reader = PHPExcel_IOFactory::createReader('Excel5');
        $this->workbook = $xls_reader->load($template_location);
    }
    
    function send()
    {
        $xls_writer = PHPExcel_IOFactory::createWriter($this->workbook, 'Excel5');
        $xls_writer->save('result/from-template.xls');    
    }
}
excel-tester.php
<?php

require_once('excel.php');

$excel = new Excel();
$excel->load();
$excel->send();

Any suggestion ?
Developer
Jan 19, 2009 at 1:25 AM
Now that you have got one case working and one case not working, I would try to add lines one by one to the working case (approaching the non-working case) until you find out exactly what makes it fail.

I am unfortunately not familiar with CodeIgniter.

Jan 19, 2009 at 1:47 AM
I've done some manual tracing before, and i found that the code execution is actually stopped at PHPExcel/Writer/Excel5/Workbook.php when it calls require_once 'PHPExcel/Writer/Excel5/Worksheet.php'.

The same problem happens to Excel2007 writer. But PDF and HTML writer goes fine for me.

Well, I decided to write the result to PDF insted of XLS.
If my clients have to edit something, they have to edit the template file instead :p

Thanks for the help ^^
I hope the bug will be fixed soon.

Developer
Jan 19, 2009 at 1:59 AM
Hmm... ok, but this sounds very strange. If you do find that this is a bug in PHPExcel, please let us know.