Methods for exporting Excel using Phpexcel in Yii _php instance

Source: Internet
Author: User
Tags autoloader spl yii

The example in this article describes a method for using Phpexcel to export excel in Yii. Share to everyone for your reference. The specific analysis is as follows:

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 method is this, the code is as follows:

Copy Code code as follows:
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, according to the following code to modify the Phpexcel code directory in the autoloader.php file, the code is as follows:

Copy Code code 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 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, the code is as follows:

Copy Code code 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 with the PHP program design 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.