Yii 2.0: 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);
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'fullExportType'=> 'xlsx', //can change to html,xls,csv and so on
            'grid_mode' => 'export',
            'columns' => [
                ['class' => 'yii\grid\SerialColumn'],

Another more elegant way are below: 1. Still create export action as above state.

  1. 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.

Total 4 comments

#20153 report it
ragua at 2017/12/22 08:29am
doesn't seems to work with related content

Didn't succeed on replace colum content by related (by example country.country_name instead of country_id).

#19656 report it
lordrebs at 2015/11/06 10:41pm
exportmenu not collapsing when click

Hi! I successfully setup the export menu and now appearing both button (columns, export option).... but the problem is whenever i click on the buttons the menu wont show up. Did I missed something? I am also using AdminLTE theme, I'm thinking whether something conflicting with the css or js. Please help

#18413 report it
Scott_Huang at 2014/10/26 02:19am
@Kartik V

That would be great if you can integerate full export function into your great grid widet!!!

#18409 report it
Kartik V at 2014/10/25 03:46am
Grid extension updates

Thanks for the article. Planning to collaborate with author of yii2-excelview to integrate this better with the yii2-grid extension (probably a new extension). Watch github page for more.

Leave a comment

Please to leave your comment.

Write new article