The example in this article describes a method for using Phpexcel to export excel in Yii. Share to everyone for your reference. The specific analysis is as follows:
Recently in the research of the YII framework of PHP, very like, encountered the problem of exporting Excel, studied, there are the following methods.
1, first in the cofig/main.php to add a reference to Phpexcel, my method is this, the code is as follows:
Copy Code code as follows:
Autoloading model and component classes
' Import ' =>array (
/* ' Application.modules.srbac.controllers.SBaseController ', * *
' Application.models.* ',
' Application.components.* ',
' Application.extensions.phpexcel.* ',
),
2, of course, remember to copy phpexcel entire directory to the project's "protected/extensions/" directory.
3, according to the following code to modify the Phpexcel code directory in the autoloader.php file, the code is as follows:
Copy Code code as follows:
public static function Register () {
/*if (function_exists (' __autoload ')) {
Register any existing autoloader function with SPL, so we don ' t get any clashes
Spl_autoload_register (' __autoload ');
}
Register ourselves with SPL
return Spl_autoload_register (Array (' Phpexcel_autoloader ', ' Load '));
$functions = Spl_autoload_functions ();
foreach ($functions as $function)
Spl_autoload_unregister ($function);
$functions = Array_merge (Array (' Phpexcel_autoloader ', ' Load '), $functions);
foreach ($functions as $function)
$x = Spl_autoload_register ($function);
return $x;
}//function Register ()
In the above function, the annotation is out of the original code.
4, the following code is output Excel, as well as some commonly used property settings, in your controller, the code is as follows:
Copy Code code as follows:
$objectPHPExcel = new Phpexcel ();
$objectPHPExcel->setactivesheetindex (0);
Ob_end_clean ();
Ob_start ();
Header (' Content-type:application/vnd.ms-excel ');
Header (' content-disposition:attachment;filename= '. ') xiaoqiang-'. Date ("YMJ"). XLS "');
$objWriter = Phpexcel_iofactory::createwriter ($objectPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
I hope this article will help you with the PHP program design based on the YII framework.