Export to Excel or CSV from CGridView

Is there a way to export the data from a CGridView to either .xls or .csv? I would like to be able to filter and sort on a CGridView, then export my results.

I have tried the csvout extension, which works ok-ish if you don’t need pagination, but for larger amounts of data I want to display the top 15 or so, but export the full result if it is more than that.

So, have any ideas?

CGridView get the data to display from CActiveDataProvider…

So depending from where you would call the EXPORT… you just need to get the grid->dataProvider->data…

this returns an array of data for the current page if page are set… so before that you would need to set pagination to false if you want all data exported… or leave as it is to get exported the data currently shown in the grid…

The most simple is csv.

foreach ($dataProvider->$data as $model)


echo $model->id.', '.$model->name.', '.$model->type;


If you want to use excel, I advice you phpExcel library.

Well it’s fairly easy to create CSV-files for Excel. You just need to remember to output the two key characters (chr(255) and chr(254)) before you output any content and then convert the encoding correctly (mb_convert_encoding($output, ‘UTF-16LE’, ‘UTF-8’)) after outputting all data.

there is a nice extension that does the job

try this article


The article gives some JavaScript code under the heading Extending CGridView javascript code. I must be dense, because I can’t figure out how to use it. Where should I paste that code? TIA.

Answering my own question, it goes inside a registerScript block in the View script, something like this:

Yii::app()->clientScript->registerScript('export', "

$('#export-button').on('click',function() {



$.fn.yiiGridView.export = function() {


        success: function() {


            window.location = '". $this->createUrl('exportFile')  . "';


        data: $('.search-form form').serialize() + '&export=true'




I encountered a problem implementing the method described in that article, and I developed a solution that’s documented in a comment on that article. I hope it helps someone. (I’m surprised there isn’t more interest in this topic. I hope the option to show an Export button may get built into a future version of CGridView.)

I am new to php and yii but I seem to be having problems with the

$this->createUrl('exportFile')  . "';

part and I am also having problems downloading the csv file.

@djviper88: i know am, late but this link will be useful…i had faced a similar issue


I don’t why, but a lot of lessons for Yii have errors.

In your case just do

$this->createUrl(\'exportFile\')  . "';

i am using this method and works but i am getting an empty file. Why?

thx in advance

The two most likely reasons based on my experience are:


[*]There’s a coding error somewhere. (Check logs, use Firebug for Javascript, etc.)

[*]In the controller it doesn’t grant permission for actionExportFile.


I inserted the registerScript block, as you suggested immediately after the existing registerScript block for the search menu associated with CGridView:

Yii::app()->clientScript->registerScript('search', "



	return false;


$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('equipment-grid', {

		data: $(this).serialize()


	return false; }); ")

After I added the new registerScript block, not only did it not work but the existing search form also failed to function. Any suggestions?