PHPExcel无法导出文件下载?

时间:2017-11-28 09:00:32

标签: php export phpexcel

我使用PHPExcel导出我的MySQL数据ta xls文件,但是当我运行它时回显一些事情而不是将文件下载到我的本地。我在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;

任何人都可以帮助我吗?读书!

1 个答案:

答案 0 :(得分:1)

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;