This article will introduce to you the specific method of using the PHPExcel plug-in in the yii framework to quickly export excel data. Students who are using yii may want to refer to it.
Recently I have been studying the Yii framework of PHP and I like it very much. When I encountered the problem of exporting Excel, I studied it and came up with the following method:
1. First add a reference to PHPExcel in cofig/main.php. My method is 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 the entire PHPExcel directory to the "protected/extensions/" directory of the project.
3. Modify the Autoloader.php file in the PHPExcel 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(array('PHPExcel_Autoloader','Load')),$functions);
foreach ( $functions as $function)
$x = spl_autoload_register($function);
return $x;
} // function Register()
|
|
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');
}
代码如下 |
复制代码 |
$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');
|
// 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(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 property settings in your Controller:
The 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');
http://www.bkjia.com/PHPjc/630690.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630690.htmlTechArticleThe article will introduce to you the specific method of using the PHPExcel plug-in to quickly export excel data in the yii framework. There are Students who use Yii may want to check it out for reference. I’ve been studying PHP recently...
|