Step by step for how to full export Yii2 grid to excel

We will leverage Yii2-excelview widget. So, first of all is install Yii2-excelview:

Either run

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

or add

"arturoliveira/yii2-excelview": "*"

to the require section of your composer.json file.

In the main.php add below if not exist before:

'modules' => [//add by Scott
        'gridview' => [
            'class' => '\kartik\grid\Module'
        // 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 your controller create an export action like below. Then you can export your data through link: yousite\yourcontroller\export

use arturoliveira\ExcelView;
    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' => 'yii\grid\SerialColumn'],
                'code',
                'name',
                'population',
              ],
        ]);
    }

Another more elegant way are below:

  1. Still create export action as above state.

  2. Change the default kartik-v export action to below

'modules' => [//add by Scott
        'gridview' => [
            'class' => '\kartik\grid\Module',
             // 'downloadAction' => 'gridview/export/download',
            'downloadAction' => 'export',  //change default download action to your own export action.
  1. Add below to your grid vidget:
'layout' => '{summary}<div class="pull-right">{export}&nbsp{fullexport}&nbsp</div><div>{items}</div>{pager}',
            'exportConfig' => [
                \kartik\grid\GridView::EXCEL => ['label' => 'Export to Excel'],
            ],
            'fullExportConfig' => [
                ExcelView::FULL_EXCEL => [],
                //ExcelView::FULL_CSV => ['label' => 'Save as CSV'],
                ExcelView::FULL_HTML => [],
            ],

Btw, you better remove {fullexport} and 'fullExportConfig' session since it is for Excelview dropdown menu only. And we try to leverage Kartik-v gridview dropdown menu, even we still use the Excelview export action. I intend list it just FYI.

Then when you click the export dropdown menu, it will auto invoke download action.

The reason I don't use excelView {fullexport} directly is excelView dropdown menu have some bugs till Oct 24,2014. So I try combine Kartik-v's export menu + ExcelView export function as tentative solution.