Krajee

Excel Export Formatting Demo

Thankful to Krajee! BUY A COFFEEor to get more out of us.
Demonstration examples and scenarios for Excel Export of data. You would see the changes only on exporting data as Excel format via the export menu button dropdown for each grid. Note that the GridView pjax property has been enabled (set to true) for all the demo examples.

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)

Auto formatting example. This example showing auto formatting of cells for excel export by setting the autoXlFormat property at the GridView widget level to true. It will use the format setting for each column to auto-derive the excel cell format. Click on export dropdown menu on top right above and select excel export.


Click for demo
echo GridView::widget([
    'dataProvider'=>$dataProvider,
    'autoXlFormat'=>true,
    'toggleDataContainer' => ['class' => 'btn-group mr-2 me-2'],
    'export'=>[
        'showConfirmAlert'=>false,
        'target'=>GridView::TARGET_BLANK
    ],
    'columns'=>[
        [
            'attribute'=>'code', 
            'format'=>'text', 
            'width'=>'100px', 
            'pageSummary'=>'Total'
        ],
        [
            'attribute'=>'name', 
            'format'=>'text', 
            'width'=>'120px'
        ],
        [
            'attribute'=>'buy_date', 
            'format'=>['date', 'php:d-M-Y'], 
            'width'=>'100px'
        ],
        [
            'attribute'=>'launch_time', 
            'format'=>['time', 'php:g:i a'], 
            'hAlign'=>'center', 
            'width'=>'100px'
        ],
        [
            'attribute'=>'quantity', 
            'format'=>'integer', 
            'hAlign'=>'right', 
            'width'=>'100px', 
            'pageSummary'=>true
        ],
        [
            'attribute'=>'price', 
            'format'=>['decimal', 2], 
            'hAlign'=>'right', 
            'width'=>'100px', 
            'pageSummary'=>true
        ],
        [
            'class'=>'kartik\grid\FormulaColumn', 
            'label'=>'Amount', 
            'format' => ['decimal', 2],
            'value'=>function ($model, $key, $index, $widget) { 
                $p = compact('model', 'key', 'index');
                return $widget->col(4, $p) * $widget->col(5, $p) ;
            }, 
            'hAlign'=>'right', 
            'width'=>'120px', 
            'pageSummary'=>true
        ],
        [
            'attribute'=>'created_on', 
            'format'=>['datetime', 'php:d-M-y H:i:s'], 
            'width'=>'140px'
        ]
    ],
    'pjax'=>true,
    'showPageSummary'=>true,
    'panel'=>[
        'type'=>'primary',
        'heading'=>'Products'
    ]
]);

Formatting each column (overrides the autoXlFormat setting). This example showing different formats for columns - i.e. scientific number format and different date or time formats.


Click for demo
echo GridView::widget([
    'dataProvider'=>$dataProvider,
    'autoXlFormat'=>true,
    'toggleDataContainer' => ['class' => 'btn-group mr-2 me-2'],
    'export'=>[
        'showConfirmAlert'=>false,
        'target'=>GridView::TARGET_BLANK
    ],
    'columns'=>[
        [
            'attribute'=>'code', 
            'format'=>'text', 
            'width'=>'100px', 
            'pageSummary'=>'Total'
        ],
        [
            'attribute'=>'name', 
            'format'=>'text', 
            'width'=>'120px'
        ],
        [
            'attribute'=>'buy_date', 
            'format'=>['date', 'php:d-M-Y'], 
            'xlFormat'=>'mmm\-dd\, yyyy',  // different date format
            'width'=>'100px'
        ],
        [
            'attribute'=>'launch_time', 
            'format'=>['time', 'php:g:i a'], 
            'hAlign'=>'center', 
            'xlFormat'=>'Long Time', // long time
            'width'=>'100px'
        ],
        [
            'attribute'=>'quantity', 
            'format'=>'integer', 
            'hAlign'=>'right', 
            'width'=>'100px', 
            'xlFormat'=>'0\.00E+00', // scientific
            'pageSummary'=>true
        ],
        [
            'attribute'=>'price', 
            'format'=>['decimal', 2], 
            'hAlign'=>'right', 
            'width'=>'100px', 
            'pageSummary'=>true
        ],
        [
            'class'=>'kartik\grid\FormulaColumn', 
            'label'=>'Amount', 
            'format' => ['decimal', 2],
            'value'=>function ($model, $key, $index, $widget) { 
                $p = compact('model', 'key', 'index');
                return $widget->col(4, $p) * $widget->col(5, $p) ;
            }, 
            'hAlign'=>'right', 
            'width'=>'120px', 
            'pageSummary'=>true
        ],
        [
            'attribute'=>'created_on', 
            'format'=>['datetime', 'php:d-M-y H:i:s'], 
            'width'=>'140px'
        ]
    ],
    'pjax'=>true,
    'showPageSummary'=>true,
    'panel'=>[
        'type'=>'primary',
        'heading'=>'Products'
    ]
]);

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Yii2 Demos since 22-May-2017