eexcelview is very good extension of PHPExcel for Yii. But document is too simple. Fortunately, there are some good example code in the forum. I also provide my simple code here to help someone first to use eexcelview. Hope it can save you a couple of hours to discover this cool tool.
This code is assume you already created model and curd code by GII. (See http://www.yiiframework.com/doc/guide/1.1/en/topics.gii)
Then we try to add the export function into admin action.
Before you can add the code, down load PHPExcel from http://phpexcel.codeplex.com/
I download version 1.7.9
and also get newest eexcelview 0.3.3 from
http://www.yiiframework.com/extension/eexcelview/
Step 1. add export button into _search.php, so you can export search result into CSV file. PHPExcel create CSV file very fast. So I use this in the sample code, you can change that to excel, pdf, html or other format.
<div class="row buttons">
<?php echo CHtml::submitButton('Search', array('name'=>'search','id'=>'search')); ?>
<?php echo CHtml::resetButton('Clear'); ?>
</div>
<!-- here is new code -->
<div class="row buttons">
<?php echo CHtml::submitButton('Export to CSV', array('name'=>'exportToExcel','id'=>'exportToExcel')); ?>
</div>
<!-- new code end -->
Step 2. add onclick response in admin.php
See below code, the new function is $(’#exportToExcel’).click(function(), this code will create extra passing HTTP request parameter export to admin action.
Yii::app()->clientScript->registerScript('search', "
$('#exportToExcel').click(function(){
window.location = '". $this->createUrl('admin') . "?' + $(this).parents('form').serialize() + '&export=true';
return false;
});
$('#search').click(function(){
$('#export').val('');
});
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#tool-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
Step 3: Add export code into actionAdmin which is in your Controller.php file
public function actionAdmin()
{
$model=new Tool('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Tool']))
$model->attributes=$_GET['Tool'];
//below is code we added for export search result to CSV file.
if(isset($_GET['export']))
{
$this->widget('ext.EExcelView', array(
'id'=>'tool-grid',
'dataProvider'=>$model->search(),
'title'=>'Report',
'grid_mode'=>'export',
'filename'=>'csvreport',
'exportType'=>'CSV',
'autoWidth'=>false,
'stream'=>TRUE, //make sure you output to browser
'columns'=>array(
//you columns from model.
...
),
));
//some people add Yii::app()->end(); here, but it's not necessary, see EExcelView.php source code.
}
$this->render('admin',array(
'model'=>$model,
));
}
Now, you can test your code, if it works, then you can add your own code based on that.