stasuss
(Stasuss)
February 27, 2012, 10:32am
1
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
mdomba
(Maurizio Domba Cerin)
February 27, 2012, 10:51am
2
NOTE: moved to proper section (Tips, Snippets and Tutorials instead of General Discussion for Yii 1.1.x)
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
shailesh
(Shailesh Makwana990)
March 22, 2012, 9:44am
6
[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]
blargism
(Mills Joe)
May 19, 2012, 11:54pm
7
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.
blargism
(Mills Joe)
May 19, 2012, 11:56pm
8
isurumadhu
(Madhu Isuru)
September 10, 2012, 10:40am
10
Thanks a lot … you save me
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.
umbalaconmeogia:
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.