Another Yii CGridView-to-Excel exporter using PHPExcel, based on EExcelView
Tested with:
Github repository: https://github.com/tellibus/tlbExcelView
This is an example of use of EExcelView in the controller and view:
Controller
Based on the standard Gii / Giix admin action
public function actionAdmin() { $model = new Model('search'); $model->unsetAttributes(); if (isset($_GET['Model'])) { $model->attributes = $_GET['Model']; } if (isset($_GET['export'])) { $production = 'export'; } else { $production = 'grid'; } $this->render('admin', array('model' => $model, 'production' => $production)); }
admin view
Yii::app()->clientScript->registerScript('search', " $('#exportToExcel').click(function(){ window.location = '". $this->createUrl('admin') . "?' + $(this).parents('form').serialize() + '&export=true'; return false; }); $('.search-form form').submit(function(){ $.fn.yiiGridView.update('some-grid', { data: $(this).serialize() }); return false; }); "); … <div class="search-form" style="display:block"> <?php $this->renderPartial('_search', array('model' => $model)); </div><!-- search-form --> … <?php $this->widget('application.components.widgets.tlbExcelView', array( 'id' => 'some-grid', 'dataProvider' => $model->search(), 'grid_mode' => $production, // Same usage as EExcelView v0.33 //'template' => "{summary}\n{items}\n{exportbuttons}\n{pager}", 'title' => 'Some title - ' . date('d-m-Y - H-i-s'), 'creator' => 'Your Name', 'subject' => mb_convert_encoding('Something important with a date in French: ' . utf8_encode(strftime('%e %B %Y')), 'ISO-8859-1', 'UTF-8'), 'description' => mb_convert_encoding('Etat de production généré à la demande par l\'administrateur (some text in French).', 'ISO-8859-1', 'UTF-8'), 'lastModifiedBy' => 'Some Name', 'sheetTitle' => 'Report on ' . date('m-d-Y H-i'), 'keywords' => '', 'category' => '', 'landscapeDisplay' => true, // Default: false 'A4' => true, // Default: false - ie : Letter (PHPExcel default) 'RTL' => false, // Default: false - since v1.1 'pageFooterText' => '&RThis is page no. &P of &N pages', // Default: '&RPage &P of &N' 'automaticSum' => true, // Default: false 'decimalSeparator' => ',', // Default: '.' 'thousandsSeparator' => '.', // Default: ',' //'displayZeros' => false, //'zeroPlaceholder' => '-', 'sumLabel' => 'Column totals:', // Default: 'Totals' 'borderColor' => '00FF00', // Default: '000000' 'bgColor' => 'FFFF00', // Default: 'FFFFFF' 'textColor' => 'FF0000', // Default: '000000' 'rowHeight' => 45, // Default: 15 'headerBorderColor' => 'FF0000', // Default: '000000' 'headerBgColor' => 'CCCCCC', // Default: 'CCCCCC' 'headerTextColor' => '0000FF', // Default: '000000' 'headerHeight' => 10, // Default: 20 'footerBorderColor' => '0000FF', // Default: '000000' 'footerBgColor' => '00FFCC', // Default: 'FFFFCC' 'footerTextColor' => 'FF00FF', // Default: '0000FF' 'footerHeight' => 50, // Default: 20 'columns' => $grid // an array of your CGridColumns ));
_search partial view
Here I've used giix extension, but you may just as well use Gii.
$form = $this->beginWidget('GxActiveForm', array( 'action' => Yii::app()->createUrl($this->route), 'method' => 'get', )); … <div class="row buttons"> <?php echo GxHtml::submitButton(Yii::t('app', 'Search')); <?php echo GxHtml::button(Yii::t('app', 'Export to Excel (xls)'), array('id' => 'exportToExcel')); </div> <?php $this->endWidget();
Total 7 comments
@banamlehsb
This error shouldn't happen if you follow the tutorial steps.
However, you can post your running code in the forum's extension page.
When i downloaded excel file. i can't open it. Excel noticed that: "Excel cannot open the file ... because the file format or file extension is not valid. ...". What's wrong?
Hi,
I got an issue when combining this extension with the groupgridview.
The merging and extraRows are not implemented yet. Hence, the display become a bit messy
Could you help me with this?
Cheers,
Daniel
@Wiwit Iwan SEP
Please explain a little more your issue and post the relevant parts of your code in the extension forum thread.
Cheers
Can anyone explain how to set the jquery scipt on view? it give me error " The system is unable to find the requested action "bonus?r=ledgers_detail" " bonus is my action on Ledgers_detail controller
@Daniel I've just tried it and it works well.
If you remove that column, do you see the pagination? Make sure:
You set the enablePagination property to true in your tlbExcelView widget call (the default value is false)
You actually have a CPagination component attached to your dataprovider.
If you don't see what I'm talking about, please use the extension forum thread to post the relevant parts of your current code (model + view).
Great extension. It solved a lot of my headache. But, one issue, I have a column like this one,
Just to display the row number. But this extension give me an error:
Any help on this?
Cheers,
Daniel
Leave a comment
Please login to leave your comment.