30 comments on “PHPExcel in Yii

        • I initially tired the version from this link (with yii framework):
          the loading into excel worked well, yet the loading excel file into phpexcel object has been throwning exception from yii autoloader.
          I tried both your method as well as yii patch with no avail.
          Just now i tried to use exact 1.7.6 version. The problem is that i cannot properly configure the phpexcel library.
          It turned to be in /protected/extentions/phpexcel1.7.6/Classes/PHPExcel
          I’ve rewrote autoloader – no way.
          when i: $objReader = PHPExcel_IOFactory::createReader(‘Excel2003XML’); yii issues:
          include(PHPExcel_IOFactory.php) [function.include]: failed to open stream: No such file or directory

          later i’ve copied phpexcel catalog and phpexcel.php into /protected/extention folder
          for writing into excel result is: /home/extractw/siteandtraffic.com/application/protected/extensions/PHPExcel.php(118): spl_autoload_call(“PHPExcel_Worksheet”)
          113 {
          114 parent::init();
          115
          116 // Initialise worksheet collection and add one worksheet
          117 $this->_workSheetCollection = array();
          118 $this->_workSheetCollection[] = new PHPExcel_Worksheet($this);
          119 $this->_activeSheetIndex = 0;
          120
          121 // Create document properties
          122 $this->_properties = new PHPExcel_DocumentProperties();
          exception: “include(PHPExcel_Worksheet.php) [function.include]: failed to open stream: No such file or directory”
          i’m kind of preflexed…

  1. Hi Angel,
    If you use this solution, you don’t have to do anything in Yii except import PHPExcel.
    If you do this and still have problems, feel free to let me know what is actually going wrong.

  2. Thanks for your attention.

    I can’t figure out what’s happening. This is is the fragment of source where I get the error:
    [quote]
    public function comprovaFitxerDeCarrega($fitxer) {
    $errors = array();

    $phpExcelPath = Yii::getPathOfAlias(‘ext.slt.phpexcel’);
    include($phpExcelPath . DIRECTORY_SEPARATOR . ‘PHPExcel.php’);

    $o = new PHPExcel();
    CVarDumper::dump(“hello”, 1, true);

    try {
    // Identificar el tipus de fitxer
    $inputFileType = PHPExcel_IOFactory::identify($fitxer);
    CVarDumper::dump(“debug2″, 1, true);
    } catch (Exception $e) {
    CVarDumper::dump(“debug3″,1,true);
    array_push($errors, “Error: ” . $e->getMessage() );
    }
    return $errors;
    }
    [/quote]
    “comprovaFitxerDeCarrega” is a function called from my controller’s actionIndex() method. I get this error: http://i.imgur.com/ZfKIk.png

    As you see, the line “$o = new PHPExcel();” (i included this line just for test; I don’t use the $o variable) executes successfully (so the PHPExcel class is found by the loader), but the line

    $inputFileType = PHPExcel_IOFactory::identify($fitxer);

    throws an error. I’m using PHP 5.3 and PHPExcel 1.7.6 (I haven’t tried with version 1.7.3c yet, the version you use). Any ideas about what steps could I follow to find the error?

    Thanks in advance.

  3. First you can replace this:

    $phpExcelPath = Yii::getPathOfAlias(‘ext.slt.phpexcel’);
    include($phpExcelPath . DIRECTORY_SEPARATOR . ‘PHPExcel.php’);

    with this:
    Yii:import(‘ext.slt.PHPExcel.PHPExcel’);

    I also attached a new Autoloader.php file for PHPExcel 1.7.6 . Please make sure you use the Autoloader for the right version.

  4. I really appreciate your help, MrSoundless, I finally got it working thanks to your advice.

    BUT I have detected a really [i]strange[/i] behaviour: if I remove the line

    $o = new PHPExcel();

    from my function, I still get the same exception when calling to PHPExcel_IOfactory::identify(). So It seems to me that this sentence does some “magic” loading some classes or libraries which are not loaded if I call directly to PHPExcel_IOFactory::identify().

    I am a PHP newbie, as you can see. Does this make any sense to you, or am I going crazy? :-)

    Anyway, thank you very much for your time!

  5. I’m glad to hear that!

    What you said there makes complete sense:
    The reason that PHPExcel_IOFactory is not recognized is because the PHPExcel Autoloader is needed to import the PHPExcel_IOFActory class. At this time the autoloader is not loaded yet, which means it also can’t import anything. The PHPExcel Autoloader is loaded as soon as the PHPExcel.php file is included. If you check out that file, you’ll see that it includes the Autoloader in there.
    The Yii::import function, includes a class ONLY when necessary. So until you call new PHPExcel, it won’t load in the PHPExcel file which means the PHPExcel autoloader is never called.

    So to fix this, you could just create an instance of PHPExcel like you did. You could also use include instead of Yii::import (as you showed in an earlier comment)

    So yeah this sounds like a good reason to use include instead of import.

  6. @Angle
    You could also use
    Yii::import(‘blah.blah’, true);

    which includes the files right away.

  7. hi, thanks for share!

    why you don`t use Yii::registerAutoloader()?

    Yii::import(‘….PHPExcel’,true);
    Yii::registerAutoloader(array(‘PHPExcel_Autoloader’, ‘Load’),true);

    thanks a lot again

  8. yep, but the problem is that we need to load
    Autoloader.php (require or via yii::import() )
    ( or PHPExcel.php, which load it)
    before call registerAutoloader.
    And if it`s happened we`ll get error with

    PHPExcel_Shared_ZipStreamWrapper::register();

    I feel, that the another solution is near, but little bit tired to search it now ((

    also, may be you clear with me, why does we need to unregister firstly Yii`s autoloader?

    as saw it many times: in your example and in the http://www.yiiframework.com/wiki/37/integrating-with-other-frameworks/ and more results from yii`s forum

    thanks in advance, good luck

  9. When you add an autoloader it gets stacked.
    So if you just have Yii your autoloader array will look like this:
    element 0 => Yii’s Autoloader

    And if you add PHPExcel’s Autoloader to it it’ll become this:
    element 0 => Yii’s Autoloader
    element 1 => PHPExcel’s Autoloader

    So if php tries to autoload a class it first checks with Yii’s autoloader. Then PHPExcel’s autoloader. The problem with Yii’s autoloader is that it returns an exception if it cna’t find anything. This means it’ll never reach PHPExcel’s Autoloader..

    By unregistering Yii’s Autoloader first, we clear the array, then add PHPExcel’s Autoloader, then add Yii’s Autoloader. After doing this the array will look like this:
    0 => PHPExcel’s Autoloader
    1 => Yii’s Autoloader

    So now if PHP tries to autoload a class, it first tries with PHPExcel’s Autoloader. Since PHPExcel’s Autoloader does not throw an exception if it can’t find anything this will work fine. If it can’t find the class, it’ll just move on to Yii’s Autoloader.

    That’s pretty much the reason why Yii’s Autoloader is unregistered before others are registered.

    Hope this helps.

  10. one more comment from me only because you are counting! haha!
    thanks man!

  11. hi,

    I am facing the problem while i try to export the data to excel using PHPExcel.
    when it is autoloader, it will give a below message:
    include(PHPExcel_Shared_ZipStreamWrapper.php) [function.include]: failed to open stream: No such file or directory

    can anyone help me on this?

    thanks.

  12. Looks like your problem is caused by 1 of those 2 options:
    1. You didn’t unpack the php files in the right folder
    2. You didn’t add your AutoLoader to the php autoloaders as explained in the article

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>