Use PHPExcel in Yii to export the Excel instance code. This article will introduce how to use the PHPExcel plug-in the yii Framework to quickly export excel data to an instance. if you are using yii, please refer to it. Recently, I am studying PHP articles to introduce how to use the PHPExcel plug-in the yii Framework to quickly export excel data. if you are using yii, please refer to it.
Recently I have been studying the Yii Framework of PHP. I like it very much. when I encountered the problem of exporting Excel, I studied it and found the following method:
1. first add a reference to PHPExcel in cofig/main. php. my method is as follows:
| The code is as follows: |
|
// Autoloading model and component classes
'Import' => array (
/* 'Application. modules. srbac. controllers. SBaseController ',*/
'Application. models .*',
'Application. components .*',
'Application. extensions. phpexcel .*',
),
|
2. copy the entire PHPExcel directory to the "protected/extensions/" Directory of the project.
3. modify the Autoloader. php file in the PHP Excel code directory according to the following code:
| The code is 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 original code is commented out.
4. the following code outputs Excel and some common attribute settings in your Controller:
| The code is 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 '); |
Bytes. Recently I have been studying PHP...