Yii Framework Forum: Exportación a excel - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Exportación a excel Rate Topic: -----

#1 User is offline   sgonzalezmo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-June 12

Posted 15 June 2012 - 05:39 AM

Hola

Soy un novato en el desarrollo de yii (no mas de un par de semanas) i tengo la siguiente duda que agradecería alguien me ayudase. He generado el código de mi aplicación mediante GII y en la vista admin me gustaría realizar una exportación de los datos a formato exel. El problema que tengo es que no se como recuperar los filtros que se han aplicado en el CGridView para en el momento de realizar la exportación mostrar lo mismo que aparece en dicho componente.

El codigo de la vista es el siguiente:

$this->menu=array(
	array('label'=>Yii::t('app', 'promotion.general.index'), 'url'=>array('index')),
	array('label'=>Yii::t('app', 'promotion.general.new'), 'url'=>array('create')),
	array('label'=>'Export to excel', 'url'=>array('excel')),
);
...
...
...
<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'promotion-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'id',
		'service_owner',
		'unit_owner',
		'company_owner',
		'client_owner',
		'title',
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>


Y el codigo del controlador este:

	public function actionExcel()
	{	
		$model=new Promotion('search');
		$model->unsetAttributes();
		
		if(isset($_GET['Promotion']))
			$model->attributes=$_GET['Promotion'];
		
		$this->toExcel($model->search());
	}


Esto me acaba generando un excel pero siempre con todo el contenido de la tabla de Promotion.

Gracias por adelantado.

att.
0

#2 User is offline   sgonzalezmo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 11-June 12

Posted 22 June 2012 - 06:00 AM

Me contesto yo mismo con la solució que he encontrado.

Primero he modificado el metodo search() de mi modelo para incluir la linia $_SESSION['promotion.excel']=$data;

public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('service_owner',$this->service_owner,true);
		$criteria->compare('unit_owner',$this->unit_owner);
		$criteria->compare('company_owner',$this->company_owner,true);
		$criteria->compare('client_owner',$this->client_owner,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('revenue',$this->revenue);
		$criteria->compare('gp',$this->gp);
		$criteria->compare('tcv',$this->tcv);
		$criteria->compare('register_date',$this->register_date,true);
		$criteria->compare('offer_date',$this->offer_date,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('category',$this->category);


		$data = new CActiveDataProvider($this, array('criteria'=>$criteria,));
		$_SESSION['promotion.excel']=$data;

		return $data;
	}


Despues he modificado el metodo actionExcel de la siguiente forma:

	public function actionExcel()
	{	
		$d = $_SESSION['promotion.excel'];	
		$this->toExcel($d, array(
			'id',
			'title',
			'register_date',
			'service_owner',
			'unitOwner.description:text:' . Yii::t('app', 'promotion.attribute.unit_owner'),
			'company_owner',
			'client_owner',
			'description',
			'status0.description:text:' . Yii::t('app', 'promotion.attribute.status'),
			'revenue',
			'gp',
			'tcv',
			'offer_date',
			'start_date',
			'category0.description:text:' . Yii::t('app', 'promotion.attribute.category'),
		),
		time());
	}


Espero le sirva de ayuda a alguien.

Saludos.
1

#3 User is offline   stilomio 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 04-June 12

Posted 22 June 2012 - 09:33 AM

View Postsgonzalezmo, on 22 June 2012 - 06:00 AM, said:

Me contesto yo mismo con la solució que he encontrado.

Primero he modificado el metodo search() de mi modelo para incluir la linia $_SESSION['promotion.excel']=$data;

public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('service_owner',$this->service_owner,true);
		$criteria->compare('unit_owner',$this->unit_owner);
		$criteria->compare('company_owner',$this->company_owner,true);
		$criteria->compare('client_owner',$this->client_owner,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('revenue',$this->revenue);
		$criteria->compare('gp',$this->gp);
		$criteria->compare('tcv',$this->tcv);
		$criteria->compare('register_date',$this->register_date,true);
		$criteria->compare('offer_date',$this->offer_date,true);
		$criteria->compare('start_date',$this->start_date,true);
		$criteria->compare('category',$this->category);


		$data = new CActiveDataProvider($this, array('criteria'=>$criteria,));
		$_SESSION['promotion.excel']=$data;

		return $data;
	}


Despues he modificado el metodo actionExcel de la siguiente forma:

	public function actionExcel()
	{	
		$d = $_SESSION['promotion.excel'];	
		$this->toExcel($d, array(
			'id',
			'title',
			'register_date',
			'service_owner',
			'unitOwner.description:text:' . Yii::t('app', 'promotion.attribute.unit_owner'),
			'company_owner',
			'client_owner',
			'description',
			'status0.description:text:' . Yii::t('app', 'promotion.attribute.status'),
			'revenue',
			'gp',
			'tcv',
			'offer_date',
			'start_date',
			'category0.description:text:' . Yii::t('app', 'promotion.attribute.category'),
		),
		time());
	}


Espero le sirva de ayuda a alguien.

Saludos.



Gracias yo debo hacer una pantallas que exporten a excel. usare esto. Saludos
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users