As this extension is not realy a view, it does not need to be in a view file. You could(or you’d better) put its code in the controller class. So when you display a gridview which you want to save to a xls file with a button, you could call the controller and in its action you can create EExcelView class and it will call the save file dialog with action.xls (filename will be the action name).
For example(in controller):
public function actionTest($export = false)
{
if($export)
{
$this->widget('EExcelView', array(
'dataProvider'=> new CActiveDataProvider(NetClientType),
));
Yii::app()->end();
} else
$this->render('view', compact('params'....));
}
with this code you could achieve what you want by adding a link with parameter ‘export’=>true.
I will soon add new features to the class including specifying filename of the downloaded file.
Thnx for opening a post to the extension Always is necesary
Oh I see … but when I saw this code
$this->widget('EExcelView', array(
'dataProvider'=> $dataprovider,
'title'=>'Title',
'autoWidth'=>false,
..... other options
));
I thought it will be in a view. Until now, I didn’t know that the code ( $this->widget(…) ) could be on a controller
so …
I’ve adapted the code but I get the following error
YiiBase::include(NetClientType.php) [<a href='function.YiiBase-include'>function.YiiBase-include</a>]: failed to open stream: No such file or directory
I guess… I have to pass something like $model->search() in the dataProvider property
How Can I pass the dataprovider of the grid to the action to export the data??
I’m explaining what i did for ‘improve’ this extension. (I hope someone find this useful. )
Please find attached the modified PHP file. This one includes what Abdul Rehman and Gustavo pointed out.
In the admin.php view:
<?php
$this->breadcrumbs=array(
'Cantones'=>array('index'),
'Manage',
);
// This array contains all the fields that WON'T be exported. I.e: Primary Keys and so on.
$items = array('canton_id');
$serialized = rawurlencode(serialize($items));
$this->menu=array(
array('label'=>'List', 'url'=>array('index')),
array('label'=>'Crete', 'url'=>array('create')),
array('label'=>'Export to Excel', 'url'=>array('excel',
'title'=>'Cantones',
'model'=>'Canton',
'items'=>$serialized,))
);
...
In the ModelController:
...
public function accessRules()
{
...
'actions'=>array('excel'),
'users'=>array('*'),
),
...
}
public function actionExcel($title,$model,$items)
{
$this->widget('application.extensions.phpexcel.EExcelView', array(
'dataProvider'=> new CActiveDataProvider($model,array('pagination'=>false)),
'title'=>$title,
'model'=>$model,
'items'=>$items,
));
Yii::app()->end();
}
...
I guess that what I’ve said so far does not required any explanation, the following does. If I’m wrong, please let me know.
As we don’t want to export foreign keys but human readable information, I’m including functions (one per each foreign key in my current model) to retrieve the latter value:
public function getProvince_id($province_id){
$data = Province::model()->findByPK($province_id);
return $data->name;
}
This is being validated here (rederRow() function):
...
// You don't want to copy this.. It's part of the attached file.
$model = new $this->model;
$method = 'get'.$column->name;
if(method_exists($model,'get'.$column->name))
$value = $model->$method($data[$row][$column->name]);
else
$value = $data[$row][$column->name];
...
This last section might be conceptually wrong but it works…
I’m super newbie in yii as well so if you can improve this extension, please do it and let the community know.
tried the last version and code and i am getting this error: Fatal error: Class ‘CException’ not found in /Users/mikaelbortenheim/Sites/PHP/bentsen.client.sysedata.no/yii1.1.7-26apr-dev/framework/YiiBase.php on line 311….
/*excel*/
public function actionStockActualExcel()
{
$criteria= $_SESSION['IngresoDetalles-criteria'];
$sort=$_SESSION['IngresoDetalles-sort'];
$model=$_SESSION['IngresoDetalles-clase'];
$columnas=$_SESSION['IngresoDetalles-columnas'];
$this->widget('application.extensions.EExcelView.EExcelView',
array(
'dataProvider'=> new CActiveDataProvider($model,array('pagination'=>false,'criteria'=>$criteria,'sort'=>$sort,)),
'columns'=> $columnas
)
);
Yii::app()->end();
}
/*view*/
public function actionStockActual()
{
$detalle=new IngresoDetalles('search');
if(isset($_GET['IngresoDetalles']))
$detalle->attributes=$_GET['IngresoDetalles'];
/*columns to show*/
$columnas= array('idIngresoDetalle',
array( 'name'=>'idIngreso',
'value'=>'$data->ingreso->programa->nombre',
'sortable'=>'true',
'header'=>'Programa',),
array( 'name'=>'idProducto',
'value'=>'$data->producto->nombre',
'sortable'=>'true',
'header'=>'Producto',),
'fecha_vto',
'lote',
'cantidad:raw:Cantidad Inicial',
'cantidad_actual', );
/*use in actionStockActualExcel*/
$_SESSION['IngresoDetalles-columnas']=$columnas;
$this->render('stockActual',array(
'detalle'=>$detalle,
'columnas'=>$columnas
));
}
model IngresoDetalles.php
public function search()
{
/*se usa en Stock Actual*/
$criteria=new CDbCriteria;
$sort=new CSort();
$sort->defaultOrder='producto.nombre';
$criteria->with=array('ingreso.programa','producto');
$criteria->compare('idIngresoDetalle',$this->idIngresoDetalle);
.................
$data= new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
'sort'=>$sort,
));
/*excel, see in controller actionStockActualExcel*/
$_SESSION['IngresoDetalles-criteria']=$criteria;
$_SESSION['IngresoDetalles-sort']=$sort;
$_SESSION['IngresoDetalles-clase']=get_class($this);
return $data;
It is possible to make this extension to create the report using a SQL function with, for example:
public function getKmParcialAcumulado() {
$veiculo = $this->veiculo;
$sql = "SELECT kmparc FROM abastecimento
WHERE veiculo = {$veiculo}
ORDER BY kmparc DESC LIMIT 0,1";
$command = Yii::app()->getDb()->createCommand($sql);
return $command->queryScalar();
}
I wish he did the query for this query and not the gridview. Can I do this extension, or is there another that can do this.
In the accessRules of protected\controllers\EmployeeController.php , I edited the array
array('allow', // allow admin user to perform 'admin' and 'delete' and 'excel' actions
'actions'=>array('admin','delete','excel'),
'users'=>array('@'),
),
and added the actionExcel in the same file
public function actionExcel($title,$model,$items)
{
$this->widget('application.extensions.phpexcel.EExcelView' , array(
'dataProvider' => new CActiveDataProvider ($model , array('pagination'=>false)),
'title'=>$title,
'model'=>$model,
'items'=>$items,
));
Yii::app()->end();
}
What do I have to do so that it does not return the CException ‘Property “EExcelView.model” is not defined.’?
Fatal error: Class ‘CException’ not found in i am getting this error… i have also downloaded PHPExcel class from phpexcel site but still it shows fatal error… any suggestion?
I tried to install and use this extension on my localhost, it’s working fine. But when I upload the script and use it on the server I got this following error.
PHP Error
ob_end_clean(): failed to delete buffer. No buffer to delete.
139 ob_end_clean();
140 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
141 header('Pragma: public');
142 header('Content-type: '.$this->mimeTypes[$this->exportType]['Content-type']);
143 header('Content-Dis
If i try to add @ infront of ob_end_clean();
@ob_end_clean;
or performing a check before doing ob_end_clean:
if (ob_get_length() > 0) {
ob_end_clean();
}
the PHP error is gone but the controller is not working, and I receive this error on my browser:
File not found
Firefox can't find the file at hxxp://website-name.com/admin/product/export.
Check the file name for capitalization or other typing errors.
Check to see if the file was moved, renamed or deleted.
what’s weird is the controller is there, and If I delete the widget call, the controller is working.
Does anybody encounter this issue or can advice what should I do?
It’s working ok on my local install, does it related to server settings?