eexcelview

A simple extension that exports dataprovider data to excel table.
65 followers

EExcelView on github

Forum thread

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, category

Other params

  • libPath - path to PHPExcel, defaults to 'ext.phpexcel.Classes.PHPExcel'
  • 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
  • stream - true/false stream to browser or save to file.
  • grid_mode - Whether to display grid or to export it to selected format. Possible values(grid, export)
  • grid_mode_var - defaults to 'grid_mode' GET var for the grid mode

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.32

  • Fixed the CButtonColumn bug "Property "CButtonColumn"."name" is not defined"...
  • Fixed the issues with multiple level of buffering(le top)
  • Fixed export buttons column bug(le top)
  • Added documetation of all properties in the extension page.

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 :
    1. onRenderHeaderCell(PHPExcel_Cell $cell, string $value)
    2. onRenderDataCell(PHPExcel_Cell $cell, $data, string $value)
    3. 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

#12378 report it
semprul at 2013/03/17 10:20am
export button?

hi,,, how to use export button? when i use this extension, they will download the file directly when i reload the page.. there is no export button, they auto-download

#12305 report it
Thureos at 2013/03/12 11:03am
Actually it is

But for the other formats, like xls.

#12301 report it
phreak at 2013/03/12 06:21am
zip

I'm not 100% sure, but thinnk that Excel2007 format is already zipped.

#12237 report it
Thureos at 2013/03/08 04:24pm
Any plans for compressing the output?

It will be a nice idea it can zip compress the output, what do you think? Will work with that.

Thanks!

#12209 report it
banamlehsb at 2013/03/06 12:09pm
can't open excel file after downloaded.

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?

#12078 report it
FlyngThunderGod at 2013/02/26 12:03pm
Thank You

thank you for this very nice extension.

no problem with the hardcoded path for phpexcel

#10833 report it
le_top at 2012/11/26 05:48pm
Modification

Hi Please check https://www.dropbox.com/s/afxbl8t0z1i5op0/EExcelView.zip .

Following your update I noticed that my version has some extra stuff (for extra configuration, translation, ...). You'll have to change 'MyCGridView' in 'CGridView'.

#10126 report it
xNicox at 2012/10/05 07:34am
encoding problem

Thanks for the tip phreak . I'll try it.

I suposse that the rule is: - all files saved in utf-8 - in layouts/main.php check that "charset=utf-8" is added to content-type meta keyword.

any other ??

#10123 report it
phreak at 2012/10/05 04:50am
encoding problem

Well almost all of my reports are in cyrillic characters so I dont think there's problem with encoding. You said that there are files in ANSI, all your files should be in utf8. Could you convert everythink in utf8 an try again ?

#10113 report it
xNicox at 2012/10/04 06:39pm
encoding problem

Hi, I have a problem with the encoding or charset. The browser let me download the file, but when I try to open with excel, it says that the format is incorrecte. I assume is an encoding problem. I can use prefectly the same program in another infrastructure ( linux). My test is in windows xp, Spanish languaje. Can anybodiy give a clue ? I have some php files saved as ANSI and other as UTF-8. My /layouts/main.php file is saved as UTF-8 and in charset META field is charset= utf-8. The browser detect UTF-8 .... I'm lost .. Best Regards

#9507 report it
phreak at 2012/08/18 08:24am
github

EExcelView is on github - https://github.com/phreakbg/EExcelView

#9439 report it
bennouna at 2012/08/10 11:04am
Minor additions to EExcelView

These are certainly not essential, but if someone needs to name the worksheet, or else:

--- EExcelView (v0.33) 
+++ EExcelView (v0.33 abennouna)
@@ -14,6 +14,10 @@
        public $subject = 'Subject';
        public $description = '';
        public $category = '';
+        // abennouna
+       public $lastModifiedBy = '';
+       public $keywords = '';
+       public $sheetTitle = '';
 
        //the PHPExcel object
        public $objPHPExcel = null;
@@ -96,6 +100,9 @@
                $this->objPHPExcel->getProperties()->setSubject($this->subject);
                $this->objPHPExcel->getProperties()->setDescription($this->description);
                $this->objPHPExcel->getProperties()->setCategory($this->category);
+                // abennouna
+               $this->objPHPExcel->getProperties()->setLastModifiedBy($this->lastModifiedBy);
+               $this->objPHPExcel->getProperties()->setKeywords($this->keywords);
            } else
                parent::init();
        }
@@ -198,6 +205,8 @@
                if($this->autoWidth)
                    foreach($this->columns as $n=>$column)
                        $this->objPHPExcel->getActiveSheet()->getColumnDimension($this->columnName($n+1))->setAutoSize(true);
+                // abennouna
+                $this->objPHPExcel->getActiveSheet()->setTitle($this->sheetTitle);
                //create writer for saving
                $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, $this->exportType);
                if(!$this->stream)
#9438 report it
bennouna at 2012/08/10 10:54am
Document properties encoding (Creator / Subject / Description)

If you notice strange characters instead of accented ones in those fields (in the generated document's properties), you'll most likely have to encode those strings into ISO-8859-1.

$locale = 'fr_FR';
date_default_timezone_set('UTC');
setlocale(LC_ALL, $locale);
$this->widget('EExcelView', array(
    'id' => 'some-grid',
    'dataProvider' => $model->search(),
    'grid_mode' => 'export',
    'title' => 'Some report - ' . date('d-m-Y - H-i-s'),
    'creator' => 'John Doe',
    'subject' => mb_convert_encoding('Etat en français généré le ' . utf8_encode(strftime('%e %B %Y')), 'ISO-8859-1', 'UTF-8'),
    'description' => mb_convert_encoding('Description en français avec des caractères accentués pour montrer que c\'est possible :-)', 'ISO-8859-1', 'UTF-8'),

Does anyone know how to deal with non-Latin languages?

PS I'm not talking about sheet contents, they're alright in UTF-8.

#9437 report it
bennouna at 2012/08/10 07:43am
Great extension

Does what it says, perfect: it was all set up in minutes!

Thanks and keep up the good job.

#9061 report it
phreak at 2012/07/17 09:09am
10x Jaggi

Fixed and uploaded new ver 0.33

#9049 report it
Jaggi at 2012/07/16 11:15am
Bug

Found a bug in renderExportButtons() - Line 252

need to move the following out of the foreach:

if($content)
    echo CHtml::tag('div', array('class'=>$this->exportButtonsCSS), $this->exportText.implode(', ',$content));

As if you set multiple exports options in exportButtons array it'll render the button row for each entry.

#8811 report it
phreak at 2012/06/29 06:50am
v 0.32

Just uploaded new ver with bugfixes. Also added some documentation to the extension page.

#8729 report it
metalmilitia at 2012/06/22 04:29am
doesn't work for me

Installed out of the box says: "Property "CButtonColumn"."name" is not defined"...

#8466 report it
Artur Oliveira at 2012/06/05 11:16pm
Bug in export buttons rendering

Hi

Great work !!!! Many improvements from the version i was using.

I found two little bugs in renderExportButtons()

Change it from:

$content[] = CHtml::link($item['caption'], '?'.$url['query'].'exportType='.$type.'&'.$this->grid_mode_var.'=export');

To:

if (key_exists('query', $url))
    $content[] = CHtml::link($item['caption'], '?'.$url['query'].'&exportType='.$type.'&'.$this->grid_mode_var.'=export');          else
    $content[] = CHtml::link($item['caption'], '?exportType='.$type.'&'.$this->grid_mode_var.'=export');

See the & before exportType ? That was the problem. The URL is not well formed and the exportType is never used The other change is because when the uri does not contain the query part it throws an error

Undefined index: query

PS: Could you please document the options in the extension page ? Thanks!!

#8274 report it
le_top at 2012/05/22 12:08pm
ob_end_clean

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.

Leave a comment

Please to leave your comment.

Create extension