By default, yii 2 doesn’t provide the function to export gridview into file like xls or csv. But we can do it by using the two modules Yii2-excelview which has ported from Yii 1 and Yii2-grid.

Requirements: Yii2-excelview and Yii2-grid .

What is Yii2-excelview and Yii2-grid ?

Yii2 GridView on steroids. A module with various modifications and enhancements to one of the most used widgets by Yii developers. The widget contains new additional Grid Columns with enhanced settings for Yii Framework 2.0. The widget also incorporates various Bootstrap 3.x styling options and has embedded support for Pjax based rendering. More detail here

If you are familiar with yii1, you may also know about this extension eexcelview , it allows to export gridview to excel file. And now, this extension has ported to Yii2 with the name yii2-excelview

Install yii2-grid

php composer.phar require kartik-v/yii2-grid "*"

Or add

"kartik-v/yii2-grid": "*"

to the require section of your composer.json file. Then run:

php composer.phar update

Install yii2-excelview

To install yii2-excelview extension, run

php composer.phar require --prefer-dist arturoliveira/yii2-excelview "*"

Or you can add the following code inside composer.json

"arturoliveira/yii2-excelview": "*"

and run composer update command

Getting started

In the main.php , put the below code if it doesn’t exist

'modules' => [//add by Scott
     'gridview' => [
          'class' => 'kartikgridModule'
     // enter optional module parameters below - only if you need to
     // use your own export download action or custom translation
     // message source
          // 'downloadAction' => 'gridview/export/download',
     // 'i18n' => []
     ],
...

In controller.php create an action calls export

use arturoliveiraExcelView;
public function actionExport() {
     $searchModel = new CountrySearch();
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     ExcelView::widget([
          'dataProvider' => $dataProvider,
          'filterModel' => $searchModel,
          'fullExportType'=> 'xlsx', //can change to html,xls,csv and so on
          'grid_mode' => 'export',
          'columns' => [
               ['class' => 'yiigridSerialColumn'],
               'code',
               'name',
               'population',
          ],
     ]);
}

There is another elegant way to to.

In the main.php file

'modules' => [//add by Scott
     'gridview' => [
          'class' => 'kartikgridModule',
          // 'downloadAction' => 'gridview/export/download',
          'downloadAction' => 'export', //change default download action to your own export action.

And add the code below to your grid widget

'layout' => '{summary}<div class="pull-right">{export}&nbsp{fullexport}&nbsp</div><div>{items}</div>{pager}',
     'exportConfig' => [
          kartikgridGridView::EXCEL => ['label' => 'Export to Excel'],
     ],
     'fullExportConfig' => [
          ExcelView::FULL_EXCEL => [],
          //ExcelView::FULL_CSV => ['label' => 'Save as CSV'],
          ExcelView::FULL_HTML => [],
],

Source: yiiframework.com