[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

'components'=>array(

    'excel'=>array(

      'class'=>'application.extensions.PHPExcel',

    ),



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)

    spl_autoload_unregister($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();

    

    $objPHPExcel->setActiveSheetIndex(0)

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

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

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

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


    $objPHPExcel->setActiveSheetIndex(0)

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

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

 

    $objPHPExcel->getActiveSheet()->setTitle('Simple');

 

    $objPHPExcel->setActiveSheetIndex(0);

     

    ob_end_clean();

    ob_start();

    

    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');

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

  }



lines




    ob_end_clean();

    ob_start();



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)

good!

Nice!

StasuSS,

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

'import'=>array(


	'application.models.*',


	'application.components.*',


	'application.extensions.*',


),

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

@jobrieniii

[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]

[/color]

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

Solution:


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/

Thx!

Thanks a lot … you save me :)

thanks

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.