How to Use the phpexcel component in yii framework]

Source: Internet
Author: User

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 ' ));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.