Welcome Guest, Not a member yet? Register   Sign In
Problem PHPExcel + CodeIgniter
#1

[eluser]ang89[/eluser]
Anyone ever use PHPExcel before ?
I'm trying to use PHPExcel library within my CI code and i found this strange behaviour.

I want to 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
Code:
<?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
Code:
<?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.
#2

[eluser]ang89[/eluser]
I know this is more to a PHPExcel related issue :p
I post it here because i didn't see this problem when i use plain PHP (no CI).

If someone here know how to use PHPExcel, please help me with this. Thanks . .
#3

[eluser]Unknown[/eluser]
I have the same problem.
<? $objWriter->save('php://output');?> This code can't run.
anybody can help us??????




Theme © iAndrew 2016 - Forum software by © MyBB