How to export Excel using PHPExcel in Yii

Source: Internet
Author: User
Tags autoloader spl php excel
This article mainly introduces how to use PHPExcel to export Excel in Yii, which involves settings for PHPExcel and Related File Modification skills in Yii. It has some reference value, for more information, see

This article mainly introduces how to use PHPExcel to export Excel in Yii, which involves settings for PHPExcel and Related File Modification skills in Yii. It has some reference value, for more information, see

This document describes how to use PHPExcel to export an Excel file in Yii. Share it with you for your reference. The specific analysis is as follows:

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:

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

I hope this article will help you design php programs based on the Yii framework.

,

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.