Export To Excel Cgridview Filtered/shorted Record In Yii.

Recently, i have find an issue to export CGridview filtered record in excel in my Yii application. I have searching out lot many thigs but i did not get exact solution which i want. In all reference tutorial, i can export CGridview record without filtered or shorted record.

Finally i got one solution to export filtered CGridview record to excel using session and toexcel extension of Yii. You can see in following search method, I have stored filtered record in session variable and return it. So whenever i will search any record details it will store in session variable. And later, i will use that session variable to export action.

public function search()


{


    // Warning: Please modify the following code to remove attributes that


    // should not be searched.





    $criteria=new CDbCriteria;





    $criteria->compare('nationality_id',$this->nationality_id);


    $criteria->compare('nationality_name',$this->nationality_name,true);


    $criteria->compare('nationality_organization_id',$this->nationality_organization_id);


    $criteria->compare('nationality_created_by',$this->nationality_created_by);


    $criteria->compare('nationality_created_date',$this->nationality_created_date,true);





    $nationality_data = new CActiveDataProvider(get_class($this), array(


        'criteria'=>$criteria,


    ));


   


    $_SESSION['nationality_records'] = $nationality_data;


    return $nationality_data;


}

Here, Assign CActiveDataProvider data to session variable.

public function actionNationalityExportToExcel()


{


    $this->toExcel($_SESSION['nationality_records'],


    array(


        //'nationality_id::SN',


        'nationality_name',


        'Rel_user.user_organization_email_id',


        'Rel_org.organization_name',


   


    ),


    'Nationality',


    array(


        'creator' => 'RudraSoftech',


    ),


    'Excel2007'


    );


}

Note : Also go throw the toexcel extension and put require class file in extension folder directory.

Using this extension you can export data in excel in multiple excel version format like 2005 and 2007.