Yii CActiveDataProvider To Excel

Download PHPExcel from http://www.codeplex.com/PHPExcel and extract it. Now you will get the following folder structure
/Classes
/Documentation
/Tests

and more files or folder.
Rename “Classes” to “phpexcel” and paste it into “protected/extension” folder.

Download yii extension http://www.yiiframework.com/extension/eexcelview/files/EExcelView0.33.zip and include it into extension.

Yii CActiveDataProvider To Excel. Add the “excel” extension into extension folder to download the sql data to excel. Using this tutorial We can download in excel file using CActiveDataprovider. It is very simple and useful.

Folder Directory

extensions/EExcelView
extensions/phpexcel

CGridview To Export Excel

config/main.php

	'import'=>array(
		'application.models.*',
		'application.components.*',
        'ext.EExcelView.*',
	),

admin.php

<?php
$this->widget('EExcelView', array(
     'dataProvider'=> $model->search(),
	'columns'=>array(
		//'employee_id',
		'emp_number',
		'emp_firstname',
		'emp_lastname',
		'emp_middle_name',
		//'emp_nick_name',
		array(
            'name'=>'job_title_code',
            'value'=>'$data->jobTitleCode->job_title'
        ),
		array(
			'class'=>'CButtonColumn',
		),
	),     
     'title'=>'Title',
     'autoWidth'=>false,
     'template'=>"{summary}\n{items}\n{exportbuttons}\n{pager}", 
));
?>

Note

convert gridview format data to excel file using method2. 
Assign Header to excel. 
Data from relation table

CActiveDataProvider To Excel

If your dirctory file have EExcelBehavior.php, use the below method also.

extensions/EExcelView
extensions/EExcelBehavior.php
extensions/phpexcel
<?php
    public function behaviors()
    {
        return array(
            'eexcelview'=>array(
                'class'=>'ext.eexcelview.EExcelBehavior',
            ),
        );
    }

Method 1

    public function actionUserreports_1(){
        /** To download excel format **/
       $criteria=new CDbCriteria;
       // condition
       $user = new CActiveDataProvider('User',array('criteria'=>$criteria));                                
       $this->toExcel($user,
               array('username','firstname','lastname','email','companyname',
              'User Type'=>'usertype', //assign header
              'usersubscription.configdescription',//data from relation
              'Created By'=>'createdby0.username'
               ),
              'User Reports',// Excel Name
               array( 'creator' => 'bsourcecode',   ),
              'Excel2007' // This is the default value, so you can omit it. 
           );                
    }

Method 2

   public function actionUserreports_2(){
	$model=new User('search');
	$model->unsetAttributes();  // clear any default values
	if(isset($_GET['User']))
		$model->attributes=$_GET['User'];
       
       $this->toExcel($model->search(),
              array('username','firstname','lastname','email','companyname',
              'User Type'=>'usertype',
              'usersubscription.configdescription',
              'Created By'=>'createdby0.username'
              ),
              'User Reports',
               array( 'creator' => 'bsourcecode',   ),
              'Excel2007' // This is the default value, so you can omit it. 
             );                

    }
?>