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:
Still create export action as above state.
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.
- Add below to your grid vidget:
'layout' => '{summary}<div class="pull-right">{export} {fullexport} </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.
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.
@Kartik V
That would be great if you can integerate full export function into your great grid widet!!!
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
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).
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.