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 way Is this:
code is as follows |
copy code |
//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. Modify the autoloader.php file in the Phpexcel code directory according to the following code:
The code is as follows |
Copy Code |
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:
code is as follows |
copy code |
$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 '); |