Yii Framework Forum: [TIP] Using PHPExcel with Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[TIP] Using PHPExcel with Yii Rate Topic: ***** 5 Votes

#1 User is offline   StasuSS 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 20-February 12
  • Location:Kaluga, Russia

Posted 27 February 2012 - 05:32 AM

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
Things are very seldom what they seem. In my experience, they’re usually a damn sight worse. (Inquisitor Titus Drake)
2

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,349
  • Joined: 12-October 09
  • Location:Croatia

Posted 27 February 2012 - 05:51 AM

NOTE: moved to proper section (Tips, Snippets and Tutorials instead of General Discussion for Yii 1.1.x)
Find more about me.... btw. Do you know your WAN IP?
1

#3 User is offline   天佑饭饭 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 09-March 12

Posted 15 March 2012 - 05:04 AM

good!
0

#4 User is offline   c.h.r.i.s 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 19-March 12

Posted 19 March 2012 - 05:12 AM

Nice!
0

#5 User is offline   @jobrieniii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-October 11

Posted 21 March 2012 - 08:12 AM

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
0

#6 User is offline   shailesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-May 11
  • Location:Ahmedabad(India)

Posted 22 March 2012 - 04:44 AM

$objPHPExcel = new PHPExcel(); We Facing Issue of Creating Rows More Than 32658 ROWS Any One Help Out

0

#7 User is offline   Joe Mills 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 25-April 12

Posted 19 May 2012 - 06:54 PM

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.
0

#8 User is offline   Joe Mills 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 25-April 12

Posted 19 May 2012 - 06:56 PM

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

#9 User is offline   znoy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 01-August 12
  • Location:Minsk

Posted 01 August 2012 - 06:00 AM

Thx!
0

#10 User is offline   Isurumadhu 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-August 12

Posted 10 September 2012 - 05:40 AM

Thanks a lot ... you save me :)
0

#11 User is offline   apih 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 01-November 12

Posted 01 November 2012 - 08:50 PM

thanks
0

#12 User is offline   umbalaconmeogia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 24-November 12

Posted 25 November 2012 - 01:20 AM

View PostJoe Mills, on 19 May 2012 - 06:54 PM, said:

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


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.
0

#13 User is offline   Nur Rochim 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 04-February 13
  • Location:Indonesia

Posted 30 May 2013 - 06:17 PM

Wow... thanks!
0

#14 User is offline   InLoveWithYii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 07-February 11

Posted 31 January 2014 - 08:15 AM

View Postumbalaconmeogia, on 25 November 2012 - 01:20 AM, said:

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.

This only works for PHP 5.3 and above.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users