[solved] problem with a function to generate excel sheet

hello everybody

first, sorry for my English is not very good, in my project, I have to generate an output to excel from data from a Gridview, I have done by request, the excel sheet is simple, in the model inside search function I have done if the user filters the data




$session=new CHttpSession;

        $session->open();

        $session['TbPersonasServicios_records']=$criteria;




in the controller i have do this




public function actionGenerarExcel()

        {

        $session=new CHttpSession;

        $session->open();


        if(isset($session['TbPersonasServicios_records'])) // with data filter by user

        {

        $model=TbPersonasServicios::model()->findAll($session['TbPersonasServicios_records']);

                 

            $content=$this->renderPartial("excelReport",array ("model"=>$model),true);

            yii::app()->request->sendFile("ExcelPersonasServicios.xls",$content);

        

        }

        else //there isn't data filter by the user

        {

             $model=TbPersonasServicios::model()->findAll();

        }

            $model=TbPersonasServicios::model()->findAll();

            $content=$this->renderPartial("excelReport",array ("model"=>$model),true);

            yii::app()->request->sendFile("ExcelPersonasServicios.xls",$content);

        

        }




in the action rules, i have enable the function "GenerarExcel"




public function accessRules()

        {

                return array(

                        array('allow',  // allow all users to perform 'index' and 'view' actions

                                'actions'=>array('index','view','BuscaPersonas','PasaPersonas','DesdeEntidad','GenerarExcel','GenerarPdf'),

                                'users'=>array('*'),




in the corresponding view, I have the file excelReport.php in the tbPersonasServicios view and i have in the menu one option to execute the report.




 array('label'=>'Excel', 'url'=>array('GenerarExcel')),



ok, when i try to execute de report, the system show me this error.




ERR_INVALID_RESPONSE

http://localhost/.../index.php?r=TbPersonasServicios/GenerarExcel




the system don`t find the action “GenerarExcel”. and i don’t know why, because i have the same process to generate a pdf report in the same controller and it is working correctly.

thanks for your time.

Under normal circumstances you don’t have to (and should not) instantiate CHttpSession. The session application component can be accessed anywhere in your application as




Yii::app()->session



just use it.

In your specific case, trying to open a session in a controller action results in error because the framework already opened one to check if user is authenticated.

Thanks a lot! for your time.

I have changed the controller following with your instructions, i have deleted the instruccion about CHttpSession.




 public function actionExcelReport()

        {

        $session= Yii::app()->session;

        

        if(isset($session['TbPersonasServicios_records']))

           

        {

        $model=TbPersonasServicios::model()->findAll($session['TbPersonasServicios_records']);

                 

            $content=$this->renderPartial("excelReport",array ("model"=>$model),true);

            yii::app()->request->sendFile("ExcelInterloc.xls",$content);

        

        }

        else

           

        {

            

            $model=TbPersonasServicios::model()->findAll();

            $content=$this->renderPartial("excelReport",array ("model"=>$model),true);

            yii::app()->request->sendFile("ExcelInterloc.xls",$content);

        }




the result is the same, where i can check?, maybe the error are in the other place, it is possible that the main.php, has something badly defined?

Thanks again.

hi! to everybody

thank you all for your time

I could solve this problem, regenerate the view with GII crud and modified the new view, of course I followed the instructions on using phtamas chttpsession within the model (thanks!).

It does not really know how it works but now it’s working.