PHPExcel无法导出要下载的文件?

PHPExcel无法导出要下载的文件?,php,export,phpexcel,Php,Export,Phpexcel,我使用PHPExcel导出MySQL数据taxls文件,但当我运行它时,会返回一些东西,而不是将文件下载到本地。我在centos 7上使用firefox 我的错误如下: ��ࡱ�;�� ?����@ABCDEFGHIJKL������������������������������������������������������������������������������������������������������������������������������������������

我使用PHPExcel导出MySQL数据taxls文件,但当我运行它时,会返回一些东西,而不是将文件下载到本地。我在centos 7上使用firefox

我的错误如下:

��ࡱ�;�� ?����@ABCDEFGHIJKL������������������������������������������������������������������������������������������������������������������������������������������������������������

这是我的密码:

foreach($this->items as $r => $dataRow) {
            $row = $baseRow + $r;
            $objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $r+1)
                ->setCellValue('B'.$row, $dataRow['a'])
                ->setCellValue('C'.$row, $dataRow['b_display'])
                ->setCellValue('D'.$row, $dataRow['c_count'])
                ->setCellValue('E'.$row, $dataRow['d'])
                ->setCellValue('F'.$row, $dataRow['e'])
                ->setCellValue('G'.$row, '=C'.$row.'*D'.$row);
        }
        $filename=mt_rand(1,100000).'.xls'; //just some random filename

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header("Content-Disposition: attachment; filename=DoanhNghiep.xls");
        header("Pragma: no-cache");
        header("Expires: 0");

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
        exit;
有人能帮我吗?谢谢阅读

I assume that your code of foreach is correct you can try this modified header code

        $filename=mt_rand(1,100000).'.xls'; //just some random filename

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header("Content-Disposition: attachment; filename='".$filename."'");
        header("Pragma: public");
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //downloadable file is in Excel 2003 format (.xls)
        $objWriter->save('php://output');  //send it to user, of course you can save it to disk also!
        exit;
或者你也可以尝试下面的代码,这是我的工作在我的项目

    $objPHPExcel->setActiveSheetIndex(0);


    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Booking Report.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed
    header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
    exit;

您的浏览器无法读取.xls文件,因此将下载这些文件。请确保您的脚本除了保存的文件外,没有向浏览器发送任何内容。。。。。很可能在此之前您发送了一些内容;检查代码中是否有echo或print语句,或者任何不在PHP标记之间的语句。。。。即使是一个新行字符也会损坏outputPS应用程序/vnd.openxmlformats-officedocument.spreadsheetml.sheet是xlsx文件的内容类型,而不是xls文件。我不知道为什么它没有下载:对于.xls,请尝试:`application/vnd.ms-excel`作为内容类型谢谢,但它仍然无法下载。它将回显到html,而不是下载: