Home  >  Article  >  Backend Development  >  How to export Excel using PHPExcel in Yii, yiiphpexcel_PHP tutorial

How to export Excel using PHPExcel in Yii, yiiphpexcel_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:39782browse

How to export Excel using PHPExcel in Yii, yiiphpexcel

The example in this article describes how to use PHPExcel to export Excel in Yii. Share it with everyone for your reference. The specific analysis is as follows:

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

Copy code 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. 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:


Copy 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()
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 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 be helpful to everyone’s PHP programming based on the Yii framework.

http://www.bkjia.com/PHPjc/933603.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/933603.htmlTechArticleHow to use PHPExcel to export Excel in Yii, yiiphpexcel This article describes the method of using PHPExcel to export Excel in Yii. Share it with everyone for your reference. The specific analysis is as follows: Recently...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn