今天yii2表格GridView使用kartik扩展yii2-export导出excel文档的时候需要在表格内容前面或后面加一些说明内容,结果官方文档说明也不是很清楚,查看vendor\kartik-v\yii2-export\src\ExportMenu.php源码发现有contentBefore和contentAfter两个属性可以用,尝试下果然可以,代码如下:
<?php echo ExportMenu::widget([ 'dataProvider' => $dataProvider, 'columns' => $columns, 'showColumnSelector' => false, 'asDropdown' => false, 'exportConfig' => [ ExportMenu::FORMAT_HTML => false, ExportMenu::FORMAT_CSV => false, ExportMenu::FORMAT_TEXT => false, ExportMenu::FORMAT_PDF => false, ExportMenu::FORMAT_EXCEL => false, ExportMenu::FORMAT_EXCEL_X => [ 'icon' => '', 'iconOptions' => ['class' => ''], 'label' => '<button id="download">导出</button>', 'options' => ['class' => 'btn btn-primary', 'style' => 'display:none'], ], ], //导出excel内容前面加内容 'contentBefore' => [ ['value' => 'v1'], ['value' => 'v2'], ], //导出excel内容后面加内容 'contentAfter' => [ ['value' => 'v1'], ['value' => 'v2'], ], 'filename' => date('YmdHi'), ]);