Export database data into excel in php

Hi, my problem is to generate a excel sheet based upon the value selected in gridview, now i’m getting all the value from database using the following command…




$model = new Employee();

$data = $model->findAll();

$objPHPExcel->setActiveSheetIndex(0)

		    ->setCellValue('B4', 'Employee No.')

		    ->setCellValue('C4', 'Employee Name')

		    ->setCellValue('D4', 'Designation')

		    ->setCellValue('E4', 'Register No.');

$i=5;

foreach($data as $record){

$objPHPExcel->setActiveSheetIndex()->setCellValue('B'.$i, $record->e_no);

$objPHPExcel->setActiveSheetIndex()->setCellValue('C'.$i, $record->e_name);

$objPHPExcel->setActiveSheetIndex()->setCellValue('D'.$i, $record->desig);

$objPHPExcel->setActiveSheetIndex()->setCellValue('E'.$i, $record->reg_no);

$i++;

}



But i want to display only the value selected from the grid, i’m getting the selected key value from the grid using $_POST[‘e_no’] but i don know how to form a code to get the particular value in this problem, so please help me friends…

Thanks in advance…

The following is the abbreviated version of my own code which I use to export a CGridView displayed data to Excel.




public function actionAdmin()

{

	$model = new Employee('search');

	$model->unsetAttributes();  // clear any default values

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

	{

		$model->attributes = $_GET['Employee'];

		// save search parameters to the session data

		Yii::app()->user->setState('EmployeeSearchParams', $_GET['Employee']);

	}

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

		'model' => $model,

	));

}


public function actionExcel()

{

	...

	$model = new Employee('search');

	$model->unsetAttributes();  // clear any default values

	// retrieve the search parameters from the session data

	$params = Yii::app()->user->getState('EmployeeSearchParams');

	if ( isset($params) )

		$model->attributes = $params;

	// retrieve the data by CActiveDataProvider

	$dataProvider = $model->search();

	$data = $dataProvider->getData();

	...

	$i=5;

	foreach($data as $record){

	{	

		$objPHPExcel->setActiveSheetIndex()->setCellValue('B'.$i, $record->e_no);

		...

		$i++;

	}	

	...

}



You know that my "admin" page has a link button to actionExcel.

Note that this won’t work as it is for your needs. It’s just a rough sketch to illustrate the idea.

The points are:

  1. Use CActiveDataProvider to retrieve the records. It makes it easier to use the same search parameters both for CGridView and Excel.

  2. You can use the session data to store/restore the search parameters.

[EDIT]

In fact, I use a submission of a form to evoke actionExcel and receive parameters with $_POST, because I want to include additional switches that work only for Excel output. Most of the inputs in the form for excel are hidden fields, and I synchronize them with the values in the search form before submitting.

Anyway, I would suggest to use CActiveDataProvider. :)

Thank you friend, i got the output what i exactly want simply by using the findAllByAttributes() function with the condition value($_GET[‘e_no’])… :P

Oh, I see. That’s simple enough. :D