[TIP] Using PHPExcel with Yii

Found elegant solution to use PHPExcel Library.

Of course download und put it under extensions folder, so the PHPExcel.php is in root of extensions, near PHPExcel folder.

then add to main.php this lines

// application components





and now just modify /protected/extensions/PHPExcel/Autoloader.php

change proc PHPExcel_Autoloader.Register as follows:

public static function Register() {

$functions = spl_autoload_functions();

  foreach($functions as $function)


  $functions=array_merge(array(array('PHPExcel_Autoloader', 'Load')), $functions);

  foreach($functions as $function)

    $x = spl_autoload_register($function);

  return $x;

}//	function Register()

now you can use PHPExcel as it described in its manual in yii controllers or models.

for example like this:

  public function actionExcel(){

    $objPHPExcel = new PHPExcel();



    ->setCellValue('A1', 'Hello')

    ->setCellValue('B2', 'world!')

    ->setCellValue('C1', 'Hello')

    ->setCellValue('D2', 'world!');


    ->setCellValue('A4', 'Miscellaneous glyphs')

    ->setCellValue('A5', 'eaeuaeioueiuyaouc');









    header('Content-Type: application/vnd.ms-excel');

    header('Content-Disposition: attachment;filename="test.xls"');

    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');






are neccessary (may be only for me) 'cause there are some garbage in the output buffer without them, and xls file is corrupted

NOTE: moved to proper section (Tips, Snippets and Tutorials instead of General Discussion for Yii 1.1.x)




Thanks for posting this - very helpful. One thing I also ran into is the fact I didn’t have extensions.* in my ‘import’ array in main.php:

// autoloading model and component classes






[-- deleted another issue I was encountering - there was no other issue – the issue was on my side --]


[color=#000000]$objPHPExcel [/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]new[/color][color=#000000] [/color][color=#660066]PHPExcel[/color]color=#666600; We Facing Issue of Creating Rows More Than 32658 ROWS Any One Help Out[/color][color=#666600]


I also ran into this and wrote a blog post. There is actually a way to do this without the loop.


public static function Register() {

    return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'), false, true);


Adding the two last parameters to spl_autoload_register. The first suppresses exceptions on failure. The second pushes this auto-loader to the top of the queue. Doing it this way keeps us from having to do the loops.

For reference here is the post blargism.wordpress.com/2012/04/27/adding-phpexcel-to-yii/


Thanks a lot … you save me :)


This does not work for me. I get the error

PHP Error[2]: include(PHPExcel_Shared_ZipStreamWrapper.php): failed to open stream: No such file or directory

The Register() method of StasuSS works well.

Wow… thanks!

This only works for PHP 5.3 and above.