Yii Framework Forum: CGridview and phpexcel - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

CGridview and phpexcel Rate Topic: -----

#21 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 10-July 12

Posted 28 September 2012 - 05:56 AM

View PostClaCS, on 10 June 2011 - 09:08 AM, said:

Hi! Yes, I did

I used the phpexcel extension and a $_SESSION var to pass the filtered data

controller.php
public function actionExcel() {

        $d = $_SESSION['Lectivo-excel'];

        $data = array();

        $data[]=array_keys($d->data[0]->attributes);//headers: cols name

        foreach ($d->data as $item) {
            $data[] = $item->attributes;
        }

        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'test');
        $xls->addArray($data);
        $xls->generateXML('filename'); //export into a .xls file
    }



model.php modify the search function
public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id_lectivo',$this->id_lectivo);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('abreviatura',$this->abreviatura,true);
		$criteria->compare('fec_inicio',$this->fec_inicio,true);
		$criteria->compare('fec_fin',$this->fec_fin,true);
		$criteria->compare('estado',$this->estado);

		$data = new CActiveDataProvider(get_class($this), array(
                        'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
                                                                        Yii::app()->params['defaultPageSize']),),
			'criteria'=>$criteria,
		));

        $_SESSION['Lectivo-excel']=$data; // get all data and filtered data :)

        return $data;
	}



view.php
<div id='menub'><?php    $this->widget('zii.widgets.CMenu', array(
                       'encodeLabel'=>false,
                       'htmlOptions'=>array(
                           'class'=>'actions'),
                           'items'=>array(
                                        ...,

                                         array(
                                           'label'=>'<img align="absmiddle" alt = "'.Yii::t('internationalization','Exportar'). '" src = "'.bUrl().'/images/icons/page_excel.png" />'. ' ' . Yii::t('internationalization', 'Exportar'),
                                           'url'=>array('Lectivo/Excel'),
                                        ),

                                       ... ,
                                        ),
                                        )
                ));
?></div></div>


copy the file attached into your extensions folder

I hope to help you

if somebody find a best way to do this ... plz share it ;)


regards!

Attachment phpexcel.zip


Hi ClaCS,

Thank you for your solutions. This is very nice code and save my time. but I've an another issues.

1. how to report the relationship data. e.g. I keep users.id in my project tbl as user_id. Now, the report exported the project.user_id only. I want to export the username at report. How can I?

2. and some of the tbl's field keep the data by csv or jason code. For those fields are relationship with other tables. how could I export the really data from relative tbl at report.

Regards
Thu Ra
0

#22 User is offline   seletar6 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 10-September 12

Posted 07 December 2012 - 12:08 AM

View PostClaCS, on 10 June 2011 - 09:08 AM, said:

Hi! Yes, I did

I used the phpexcel extension and a $_SESSION var to pass the filtered data

controller.php
public function actionExcel() {

        $d = $_SESSION['Lectivo-excel'];

        $data = array();

        $data[]=array_keys($d->data[0]->attributes);//headers: cols name

        foreach ($d->data as $item) {
            $data[] = $item->attributes;
        }

        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'test');
        $xls->addArray($data);
        $xls->generateXML('filename'); //export into a .xls file
    }



model.php modify the search function
public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id_lectivo',$this->id_lectivo);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('abreviatura',$this->abreviatura,true);
		$criteria->compare('fec_inicio',$this->fec_inicio,true);
		$criteria->compare('fec_fin',$this->fec_fin,true);
		$criteria->compare('estado',$this->estado);

		$data = new CActiveDataProvider(get_class($this), array(
                        'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
                                                                        Yii::app()->params['defaultPageSize']),),
			'criteria'=>$criteria,
		));

        $_SESSION['Lectivo-excel']=$data; // get all data and filtered data :)

        return $data;
	}



view.php
<div id='menub'><?php    $this->widget('zii.widgets.CMenu', array(
                       'encodeLabel'=>false,
                       'htmlOptions'=>array(
                           'class'=>'actions'),
                           'items'=>array(
                                        ...,

                                         array(
                                           'label'=>'<img align="absmiddle" alt = "'.Yii::t('internationalization','Exportar'). '" src = "'.bUrl().'/images/icons/page_excel.png" />'. ' ' . Yii::t('internationalization', 'Exportar'),
                                           'url'=>array('Lectivo/Excel'),
                                        ),

                                       ... ,
                                        ),
                                        )
                ));
?></div></div>


copy the file attached into your extensions folder

I hope to help you

if somebody find a best way to do this ... plz share it ;)


regards!

Attachment phpexcel.zip


Nice solution ClaCS! Thanks a lot.
0

#23 User is offline   keyta 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 16-February 12

Posted 20 August 2013 - 04:40 AM

View PostClaCS, on 10 June 2011 - 09:08 AM, said:

Hi! Yes, I did

I used the phpexcel extension and a $_SESSION var to pass the filtered data

controller.php
public function actionExcel() {

        $d = $_SESSION['Lectivo-excel'];

        $data = array();

        $data[]=array_keys($d->data[0]->attributes);//headers: cols name

        foreach ($d->data as $item) {
            $data[] = $item->attributes;
        }

        Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'test');
        $xls->addArray($data);
        $xls->generateXML('filename'); //export into a .xls file
    }



model.php modify the search function
public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id_lectivo',$this->id_lectivo);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('abreviatura',$this->abreviatura,true);
		$criteria->compare('fec_inicio',$this->fec_inicio,true);
		$criteria->compare('fec_fin',$this->fec_fin,true);
		$criteria->compare('estado',$this->estado);

		$data = new CActiveDataProvider(get_class($this), array(
                        'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',
                                                                        Yii::app()->params['defaultPageSize']),),
			'criteria'=>$criteria,
		));

        $_SESSION['Lectivo-excel']=$data; // get all data and filtered data :)

        return $data;
	}



view.php
<div id='menub'><?php    $this->widget('zii.widgets.CMenu', array(
                       'encodeLabel'=>false,
                       'htmlOptions'=>array(
                           'class'=>'actions'),
                           'items'=>array(
                                        ...,

                                         array(
                                           'label'=>'<img align="absmiddle" alt = "'.Yii::t('internationalization','Exportar'). '" src = "'.bUrl().'/images/icons/page_excel.png" />'. ' ' . Yii::t('internationalization', 'Exportar'),
                                           'url'=>array('Lectivo/Excel'),
                                        ),

                                       ... ,
                                        ),
                                        )
                ));
?></div></div>


copy the file attached into your extensions folder

I hope to help you

if somebody find a best way to do this ... plz share it ;)


regards!

Attachment phpexcel.zip


Good Job
0

#24 User is offline   RakhiKasat 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 03-December 13

Posted 03 December 2013 - 05:03 AM

Hello,
Did u find any solution for this problem ??
I am also facing the same.why xls contain xml data ??

like :
<Table>
<Row>
<Cell><Data ss:Type="String">No</Data></Cell>
<Cell><Data ss:Type="String">User Id</Data></Cell>
<Cell><Data ss:Type="String">User Name</Data></Cell>
<Cell><Data ss:Type="String">User Alias</Data></Cell>
<Cell><Data ss:Type="String">User Password</Data></Cell>
<Cell><Data ss:Type="String">Role Description</Data></Cell>
<Cell><Data ss:Type="String">User Status</Data></Cell>
<Cell><Data ss:Type="String">Date Created</Data></Cell>
<Cell><Data ss:Type="String">Last Updated date</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">1</Data></Cell>
<Cell><Data ss:Type="String">1</Data></Cell>
<Cell><Data ss:Type="String">januar</Data></Cell>
<Cell><Data ss:Type="String">ID - xxxx - xxxx</Data></Cell>
<Cell><Data ss:Type="String">xxxxxxxx</Data></Cell>


thank you.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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