How to export the data of a gridview with filter and sort?

I want to export the data of a gridview to a file with filter and sort.But I haven’t found a way to get filter/sort condition of the gridview while using ajax update.Any one can help me? Please give me some advice.Thanks!

what are you updating?

I can get the URI of the gridview using “document.getElementById(‘MyGridId’).baseURI” on firefox. But it doesn’t work on IE8.

instead of trying to get the data from front end, i would suggest making another ajax call for exporting the data. Taking data from the page directly will also not allow you to export complete data in case of more than one page in the grid.

I use PHPExcel extension to download/export the data in the grid to an excel file.

In order to make sure that the exported data is as per filter and sorting criteria, store the data in session variable in the model search function

something like this -

$_SESSION['filtered_data'] = new CActiveDataProvider(get_class($this), array(




Now let’s assume you have a button to export the data, and that button is associated with an ajax call to an action exportData in your controller. In that method actionExportData, get the data to export from the session variable -

$dataProvider = $_SESSION['Filtered_data'];

finally using PHPExcel extension is really simple -

$xls = new JPhpExcel('UTF-8', false, 'test');


$xls->generateXML('Exported Data');

where $data is a two dimensional array which you can form using the data from $dataProvider object.

The explanation is a lengthy one. I am sometimes out of word optimization ;)

Thank you, Mukesh. It works well. I prefer to store the query string or criteria instead of data while handling with a big data.

Hi Mukesh,

In my case, the user is able to sort the gridview by clicking the header of the desired column.

How can I export the data acording the sort performed by the user? Right not, It is exported using the default order…

This is my search function:

public function search()


$criteria=new CDbCriteria;













$data = new CActiveDataProvider($this, array(



Yii::app()->session[‘user-grid’] = new CActiveDataProvider(get_class($this), array(




return $data;


Many thanks!

I am managing a lot of products so I did this a little bit different.

In the search function of the model I just save in the session the sorting and the criteria like this:

        Yii::app()->session['operation-grid'] = array(

            'criteria' => $criteria,

            'sort' => $sort


and in the export function I created again a DataProvider with the criteria and sorting from before:

        $criteria = $_SESSION['operation-grid']['criteria'];

        $sort = $_SESSION['operation-grid']['sort'];

        $dataProvider = new CActiveDataProvider('OperacionesBasica', array(

            'criteria' => $criteria,

            'sort' => $sort,

            'pagination' => FALSE


This way I am not storing to much information inside the session.

take care !