Add Header to make file download




    public function actionExport()

    {

        $phpExcelPath = Yii::getPathOfAlias('application.vendors.PHPExcel.Classes');


        // 关闭YII的自动加载功能,改用手动加载,否则会出错,PHPExcel有自己的自动加载功能

        // YII框架对于组件的自动加载,要求类名与文件名一致;

        // 而PHPExcel类对应的文件名包含了上级目录名称,如:IOFactory类对应的文件名为PHPExcel_IOFactory.php

        spl_autoload_unregister(array('YiiBase','autoload'));

        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');

        // Create new PHPExcel object

        $objPHPExcel = new PHPExcel();  

        // Set properties        

        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")

                                     ->setTitle("Office 2007 XLSX Test Document")

                                     ->setSubject("Office 2007 XLSX Test Document");

        // Add some data

        $objPHPExcel->setActiveSheetIndex(0)

        ->setCellValue('A1', 'Hello')

        ->setCellValue('B2', 'world!')

        ->setCellValue('C1', 'Hello')

        ->setCellValue('D2', 'world!');

        // Rename sheet

        $objPHPExcel->getActiveSheet()->setTitle('Simple');

        // Set active sheet index to the first sheet, so Excel opens this as the first sheet

        $objPHPExcel->setActiveSheetIndex(0);        

        // Redirect output to a client’s web browser (Excel5)

        header('Content-Type: application/vnd.ms-excel');        

        header('Content-Disposition: attachment;filename="01simple.xls"');

        header('Cache-Control: max-age=0');        

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $objWriter->save('php://output');

        exit;        

        Yii::app()->end();

        //恢复Yii自动加载功能

        spl_autoload_register(array('YiiBase','autoload'));

    }



2045

未命名.jpg

I want to save the .xls file, but I get some garbled, you can see the output in that picture, please tell me where is the problem, thanks.

You better post it in the forum of your language… :rolleyes:

I see, but in the Chinese forum there is no reply , and I’m very anxious.

I just want to get some help from the forum.

Can you see if this comment helps at all?

http://www.php.net/manual/en/function.header.php#83384