Working with Yii active record

Topics: Developer Forum
Jul 21, 2013 at 6:29 PM
When I use phpexcel without an active record, works fine. But, when I load an active record, the problem appears. The excel generated cannot be opened due to errors inside it.

In a simple controller (with no filters at all), this works:
public function actionIndex()
    {
        Yii::import('ext.phpexcel.PHPExcel');
        $objPHPExcel = new PHPExcel();
        
        $objPHPExcel->getProperties()->setCreator("Name")
                             ->setLastModifiedBy("Name");
        
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "Firstname");
        $objPHPExcel->getActiveSheet()->setCellValue('B1', "Lastname");
        $objPHPExcel->getActiveSheet()->setCellValue('C1', "Phone");
        $objPHPExcel->getActiveSheet()->setCellValue('D1', "Fax");
        $objPHPExcel->getActiveSheet()->setCellValue('E1', "Is Client ?");

        $objPHPExcel->setActiveSheetIndex(0);
    
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="your_name.xlsx"');
        header('Cache-Control: max-age=0');  
        
        $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
        $writer->save('php://output');
    }
And loading some AR, this doesn't work:
public function actionIndex()
    {
        $model=MyModel::model()->findAll();
        
        Yii::import('ext.phpexcel.PHPExcel');
        $objPHPExcel = new PHPExcel();
        
        $objPHPExcel->getProperties()->setCreator("Name")
                             ->setLastModifiedBy("Name");
        
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "Firstname");
        $objPHPExcel->getActiveSheet()->setCellValue('B1', "Lastname");
        $objPHPExcel->getActiveSheet()->setCellValue('C1', "Phone");
        $objPHPExcel->getActiveSheet()->setCellValue('D1', "Fax");
        $objPHPExcel->getActiveSheet()->setCellValue('E1', "Is Client ?");

        $objPHPExcel->setActiveSheetIndex(0);
    
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="your_name.xlsx"');
        header('Cache-Control: max-age=0');  
        
        $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
        $writer->save('php://output');
    }
Also I have tried modifying phpexcel autoload, and other methods on different blogs but I can't get it work
Jul 23, 2013 at 3:34 PM
Today, with no changes on the controller, it works fine. Buen when I try to do the same on a controller with filters (access control filters), with copy and paste, on the new controller doesn't work. The generated excel says that there is some problem with it and cannot be oppened.
Jul 26, 2013 at 3:13 AM
Edited Jul 26, 2013 at 3:14 AM
I guess is something with YII. When the file is saved on the disk, it opens fine; when downloaded, not working.
Jul 26, 2013 at 3:22 AM
Solved adding
header('Pragma: public');
ob_clean();
So, the part to download is:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
header('Pragma: public');
ob_clean();