How to Export Data into Excel

I wanna ask about exporting data which is displayed in Yii into Excel…

Let’s say i have this code


public function actionExcel()

    {

		

		$hasil[0]=array('propinsi'=>'propinsi',count('id_dokumen')=>'Antar',count('tgl_masuk')=>'Respon');

		

		Yii::import('application.extensions.phpexcel.JPhpExcel');

		$xls = new JPhpExcel('UTF-8', false, 'test');

		$xls->addArray($hasil);

		$xls->generateXML('report');


	}

where in the other function, i’ve put this SQL in order to be viewed as an independent relation (table)…


public function actionTables()

	{

		

		$sql='SELECT b.propinsi, COUNT(a.id_dokumen) AS Antar, COUNT(a.tgl_masuk) AS Respon

			  FROM dokumen AS a , master_prop AS b

			  WHERE a.kode_prop = b.kode_prop

			  GROUP BY propinsi';

		

		$dataProvider6=new CSqlDataProvider($sql,array(

                            'keyField' => 'Antar',

                    ));

		

		$this->render('tables',array(

			'dataProvider6'=>$dataProvider6,

		));

	

	}



when i ran this, the excel file could be downloaded, but there’s no content inside, except the attributes (propinsi and Respon)… Could anyone help me? Thanks in Advance

Hi,

have a look on this extension

this is working for me





                $model=new Report;

		$data[]=array('Patient Name','MR #','Episode','Missed Visits');

		

		foreach($model->getMissedvisit() as $list) 

		{$data[]=array($list['patient_lname'].$list['patient_fname'],$list['patient_medrec_no'],$model->getEpisodedate($list['patient_id'],'episode_start_date').'-'.$model->getEpisodedate($list['patient_id'],'end_date'),$model->getMissedcount($list['patient_id'])

					

			);

		}

		Yii::import('application.extensions.phpexcel.JPhpExcel'); 

                $xls = new JPhpExcel('UTF-8', false, 'test'); 

                $xls->addArray($data); 

                $xls->generateXML('Missed Visit'.date('Y-m-d'));[/left]



I’m sorry, but i didn’t get your point… :D v

i mean the problem is with ur array…

so, what am I suppossed to do with the array? could you give me an example?





$hasil[0]=array(count('id_dokumen'),count('tgl_masuk'));




try this

or print ur array and post here

I tried yr code, and it’s showed the value (2 column, each filled “1”)…

but, the value must be returned is 8 and 2…

I’ve modified my code just like this :


public function actionExcel()

    {       


        $sql='SELECT b.propinsi, COUNT(a.id_dokumen) AS Antar, COUNT(a.tgl_masuk) AS Respon

			  FROM dokumen AS a , master_prop AS b

			  WHERE a.kode_prop = b.kode_prop

			  GROUP BY propinsi';

                                

        if(isset($_GET['sql']))

        {

			$dataProvider6=new CSqlDataProvider($sql,array(

                            'keyField' => 'Antar',

                    ));

            

			$data = $dataProvider6->data;

            foreach ($data as $sql)

            {

                $test[] = $sql;

            }

            

			Yii::import('application.extensions.phpexcel.JPhpExcel');

            $xls = new JPhpExcel('UTF-8', false, 'test');

            $xls->addArray($test);

            $xls->generateXML('export');


        }

    }

but, when i ran this, the blank page is showed. Could you give me an advice? thanks