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…
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…
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:
Use CActiveDataProvider to retrieve the records. It makes it easier to use the same search parameters both for CGridView and Excel.
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.