Export grid without widget

I tried to find, without success, how to export a filtered grid in Yii2 without using a plugin. All I find is examples with Kartik’s export widget. I have no problem exporting the result of a query in phpexcel but I don’t understand how to deal with a dataprovider. Ideally, I want to take an array of all the data in a filtered grid, not just a page and loop through it to get my data into phpexcel or simpoly export it to csv. So let’s say I have a simple product model with a search model and I want to dislay just the ‘id’ in a view. And let’s say I have a filterd grid with 100 products. In my view, I’d have something like:

foreach ($dataprovider->models as products) {

echo products->id;


And in my controller, my dataprovider would get the search results (like the default index controller created by gii for a grid). All I can get is the fist 10 products (this is the default page size). I just need a tip to put me on track on how to achieve this, as I looked at the doc for Activedataprovider, but could’nt find the answer there.

thanks a lot!

i have the same issue

I finally got it:

In the view, I have a foreach loop with the $dataprovider

$objPHPExcel = new \PHPExcel();


		foreach ($dataProvider->models as $model) {


		$objPHPExcel->getActiveSheet()->setCellValue('A' . $row,  $model->id);

		$objPHPExcel->getActiveSheet()->setCellValue('B' . $row,  $model->title);		



In my Index file (Gridview), I have the export button:

        $form = ActiveForm::begin([

                    'action' => ['exportall'],

                    'method' => 'get',



        <div class="form-group">

            <?= Html::submitButton('Export', ['class' => 'btn btn-primary']) ?>



This is my Index action, in controller

public function actionIndex()



		if (count(Yii::$app->request->queryParams)==0)

		Yii::$app->request->queryParams = Yii::$app->session->get('IndexParams');


		Yii::$app->session->set('IndexParams', Yii::$app->request->queryParams);



		$searchModel = new ProductSearch;

		$dataProvider = $searchModel->search(Yii::$app->request->queryParams);



		return $this->render('index', [

			'dataProvider' => $dataProvider,

			'searchModel' => $searchModel,



And then, my export action, also in controller

public function actionExportall(){

		$searchModel = new ProductSearch;



		$dataProvider = $searchModel->search(\Yii::$app->session->get('IndexParams'));

		$dataProvider->pagination = false;			

		return $this->renderPartial('exportall', [

			'dataProvider' => $dataProvider,

			'searchModel' => $searchModel,



With this, I can export all the filtered data in an Excel file and format it the way I want.

Hello stromgol, you are using any extension or native library PHPExcel?

How did?

I’m not using an extension, I simply installed Phpexcel through composer like this:

"phpoffice/phpexcel": "1.8.0",

After that it’s as easy as this:

$objPHPExcel = new \PHPExcel();

And then, you need to set a correct header like this one in your view file taht you call with a renderpartial in your controller:

header('Content-Type: application/vnd.ms-excel');

$filename = "Product_" . str_replace(" ", "-", $model->id . "_" . date("d-m-Y-H:i") . ".xls");

header('Content-Disposition: attachment;filename=' . $filename . ' ');

header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');


For the rest, it works exactly like stated in Phpexcel documentation. And you have a lot more options for formating your Excel file that you would have with an extension like Kartik’S one (even though it’s a great extension).

thanks stromgol