A simple extension that exports dataprovider data to excel table.
This is my first extension ever :). It is a wrapper of PHPExcel that extends from CGrivView. The main idea is to easily export already defined grids to excel files. You can use the same array of parameters that the grid uses plus an aditional specific params, which are:
Configuraiton ¶
Document properties
- creator - The creator of the document
- title - Title of the excel document
- subject
- description
Other params
- autoWidth - whether to auto extend cells for the content(default true)
- exportType - the type of the export, all possible types of PHPExcel lib(Excel5, Excel2007,PDF, HTML)
- disablePaging - if set to true, it will export all data (default true)
- filename - the full path of the filename to export to. If null it will stream to the browser
Requirements ¶
Yii 1.1 or above and PHPExcel library
Usage ¶
The usage is like using CGridView:
$this->widget('EExcelView', array(
'dataProvider'=> $dataprovider,
'title'=>'Title',
'autoWidth'=>false,
..... other options
));
Note ¶
The path of the PHPExcel library is hardcoded to: application.extensions.phpexcel.Classes.PHPExcel
Asuming that you have extracted the lib under extensions folder. I will change that later.
ver 0.31
Mainly bug fix release. I also modified the extension to be able to run in console mode(although its a widget). I realy needed something to send me reports in xls. Here is the code snippet:
$factory = new CWidgetFactory();
$widget = $factory->createWidget($this, 'EExcelView', array(
'dataProvider'=>$provider,
'grid_mode'=>'export',
'title'=>'Title',
'filename'=>'report.xlsx',
'stream'=>false,
'exportType'=>'Excel2007',
'columns'=>array(
'col1',
'col2',
),
));
$widget->init();
$widget->run();
ver 0.3
Major change EExceView now can fully replace CGridView. There is an optional parameter 'gridmode'(default "grid") and when not set it acts just like normal grid. If gridmode="export" it exports the grid in the selected "exportType"(default "Excel5"). The path to PHPExcel can now be customized via "libPath" variable. If the lib is not found a warning is generated and export is disabled.
- Added: "gridmode" possible values(grid, export). When set to grid acts like normal grid.
- Added: "libPath" property, defaults to "application.extensions.phpexcel.Classes.PHPExcel". The location of PHP Excel lib.
- Added: "grid_mode_var" the GET variable name which can be used to change the grid mode. defaults to "grid_mode"
- Added: new template item "exportbuttons" which renders export links. Example:
....
'template'=>"{summary}\n{items}\n{exportbuttons}\n{pager}"
....
- Added: config for export buttons.
"exportButtonsCSS"(default="summary") css class for the div which wraps the buttons. "exportButtons" - array of exportTypes, for each item it renders link which exports the grid. If you specify an array you can customize the caption of the link. All export type have default captions. Example: array('Excel5'=>array('caption'=>'*.xls'), PDF). Property exportText which defines the text before the links, deault = "Export to: ".
- Added: renders CLinkColumns. Just the text of the links.
- Added: support for footer, just like normal footer.
- Added: callback functions called after processing of the cell.$cell is the php excel cell object. Refer to php excel documentation for help. $data is the same var the grids uses for expressions. $value is the text the extension had put in the cell :
- onRenderHeaderCell(PHPExcel_Cell $cell, string $value)
- onRenderDataCell(PHPExcel_Cell $cell, $data, string $value)
- onRenderFooterCell(PHPExcel_Cell $cell, string $value)
Thanks to all who have written me with ideas how to extend this extension. Sorry by I've been realy busy lately so I didnt have time to bring this earlier. I have tested the extension but if you find any bug or something you'd like me to add, feel free to write. Happy yiister :)
ver 0.2
- Fixed: Error when saving to filename.
- Fixed: bug in column indexing when > 26
- Added: support for CSV
- Added: new param "stream"(default true) when set to true streams to browser else save to local filename
- Added: get real column headers
- Added: get title from page title when not specified
Total 20 comments
The extra output was due to my setup having multiple levels of views embedded in each other. By adding: private static function cleanOutput() { for($level=ob_get_level();$level>0;--$level) { @ob_end_clean(); } }
gleaned from the ErrorHandler and calling 'cleanOutput' in stead of 'ob_end_clean' inside 'run', document creation works better.
Hi I am now testing the extension ;-). Making it work for all users still requires some work ;-=.
Some reports on issues (maybe fixes later): - When adding the 'Export' button by adjusting the template, the button opens http://site/index.php?r=controller/actionexportType=Excel2007&grid_mode=export . The '&' is missing between 'action' and 'exportType'; I changed that to $content[] = CHtml::link($item['caption'], Yii::app()->controller->createUrl('',array_merge($_GET,array('exportType'=>$type,$this->grid_mode_var=>'export')))); But even then, the 'grid-id' information is missing from the link to make sure that the link is only applied to 1 grid on the page; - When exporting, any output "rendered" before getting to the grid rendering, and after the grid rendering, is also rendered. This invalidates the file format. It may be possible to clear the output buffer before the grid, and surely avoid the rendering after. - My 'buttons column' makes the 'export' fail. The error is '"CButtonColumn.value"' is not defined. The definition for this column looks like this: array( 'template' =>'{update}', 'header'=>Yii::t('app','header.configure'), 'buttons'=>array( 'update'=>array( 'click'=>'js:function(){updateAlert(jQuery(this).parents(".grid-view").first().attr("id"));return true;}', //'imageUrl'=>'', 'label'=>Yii::t('app','header.configure'), ), ), )
Well it was GPL first, then I updated the site with MIT but have forgotten to update the file. Feel free to use it under MIT license I will udate it .
Hi Thanks for the extension (I haven't tried it yet). The web page says 'MIT' license, but the file still says 'GPL'. Kind regards
thnx valery.merlet . I was thinking about adding some types of columns handling to the extension but I thought that it would be more easy to create callback functions so that you can do whatever custom modification to the the cell you want. So in your case you could just use onRenderDataCell(PHPExcel_Cell $cell, $data, string $value) and then $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
Anyway I may add your modification in the next release :) 10x for using EExcelView.
I needed to add an option to format the value of a cell. This option is useful for text that contains a value, but must remain a digital text format in the Excel file. Here's how I modified the method 'renderRow ()': start at row 147 in the file
Now i can use the property 'type' description column :
so that the value '09548 'is correctly written in the cell
Thanks, Now I can use it in commercial projects in my work.
I modified the license to MIT. Have a nice day :)
here is the extension, I just needed this to be able to run in console so I delayed it :) @undsoft well I can change the licence if that will make you happy. I dont realy care, I give it for free to the Yii comunity and want nothing in return. I'll be happy if it helps you and makes you life easier.
It's more then tomorrow already. Have you forgot about us? :)
Also, why do you user GPL license? Why not MIT or at least LGPL?
You may put it anywhere as long as it has path to it :) or you use path alias when creating the widget.
PP: I have removed the download of the extension since I found a minor bug :) I'll get it back online tomorrow.
Thanks for this extension, it looks like just what I needed, but where do I store EExcelView.php?
I have extracted PHPExcel at extensions/phpexcel/
Thanks.
Hi phreak, I uploaded some modifications to the code improved by francies, please take a look.
Forum link
Thanks!
Hi i modified eexcelview to support footer and db relationships http://francisja.tumblr.com/post/18486330299/extended-eexcelview-with-relational-database-and-footer
Hello i modified the code a little bit to add renderFooter functionality
paste.org/pastebin/view/40197
Please add installation documentation too.
Thanks.
Thank you for your efforts developing such a nice extension.
But I have a problem. I have column which holds a text consisting of a series of numbers so it will be automatically formatted as numerical value, i.e. 12.333E+12. Is there any way to prevent this?
Thanks
Thanks for the effort and to add a couple of suggestion I've made
Will try it as soon as I need again
Gustavo
Just posted a new version of the extension. I've used almost all of the recommendations you made and a couple of things from me. Please comment.
Hello
I made a new modification
incorporate relationships
the code is adapted from code zii
hopefully useful
see changes
Leave a comment
Please login to leave your comment.