Use Phpexcel to export Excel instance code in Yii

Source: Internet
Author: User
Tags foreach autoloader spl php file yii

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

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.