Phpexcelhttp: // www.yiiframework.com/extension/phpexcel
Decompress the downloaded phpexcel package to the yii framework directory framework \ vendors.
CodeAs follows:
/* *
* Export data to excel
*/
Public Function Actionexport ()
{
// Data to be exported to excel
$ Criteria = $ This -> _ Getcriteria ();
$ Data = Statistics :: Model () -> Findall ( $ Criteria );
// Obtain the phpexcel Reference Path
$ Phpexcelpath = Yii :: Getpathofalias ( ' System. Vendors ' );
// Disable the automatic loading function of yii. Use manual loading instead. Otherwise, an error occurs. phpexcel has its own automatic loading function.
// The yii framework automatically loads components. The class name must be consistent with the file name;
// The Name Of The phpexcel class contains the parent directory name. For example, the name of the iofactory class is phpexcel_iofactory.php.
Spl_autoload_unregister ( Array ( ' Yiibase ' , ' Autoload ' ));
Include ( $ Phpexcelpath . Directory_separator . ' Phpexcel. php ' );
// The following is the Excel data export processing logic.
$ Objphpexcel = Phpexcel_iofactory :: Load ( ' ./Content/template/report.xlsx ' );
$ Objphpexcel -> Getproperties () -> Setcreator ( " Kalman " )
-> Settitle ( " Statistical Report " )
-> Setsubject ( " Statistical Report " )
-> Setdescription ( " Statistical Report " );
$ Objphpexcel -> Setactivesheetindex ( 0 )
-> Setcellvalue ( ' A1 ' , ' Hello ' )
-> Setcellvalue ( ' B2 ' , ' World! ' )
-> Setcellvalue ( ' C1 ' , ' Hello ' )
-> Setcellvalue ( ' D2 ' , ' World! ' );
$ Objphpexcel -> Setactivesheetindex ( 0 )
-> Setcellvalue ( ' A25 ' , ' 123456 ' );
$ Objphpexcel -> Getactivesheet () -> Settitle ( ' Report ' );
// Worksheet displayed after opening in Excel
$ Objphpexcel -> Setactivesheetindex ( 0 );
// Excel report output via browser
Header ( ' Content-Type: Application/vnd. openxmlformats-officedocument.spreadsheetml.sheet ' );
Header ( ' Content-Disposition: attachment; filename = "report.xlsx" ' );
Header ( ' Cache-control: Max-age = 0 ' );
$ Objwriter = Phpexcel_iofactory :: Createwriter ( $ Objphpexcel , ' Excel2007 ' );
$ Objwriter -> Save ( ' PHP: // output ' );
Yii :: APP () -> End ();
// Restore automatic yii Loading
Spl_autoload_register ( Array ( ' Yiibase ' , ' Autoload ' ));
}