Home > Backend Development > PHP Tutorial > Using PHPExcel to export Excel example code in Yii_PHP tutorial

Using PHPExcel to export Excel example code in Yii_PHP tutorial

WBOY
Release: 2016-07-13 17:06:32
Original
911 people have browsed it

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:

          return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));*/          $functions = spl_autoload_functions();
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
              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');

www.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template