繁体   English   中英

PHPExcel不能导出文件下载吗?

[英]PHPExcel can not export file to download?

我使用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;

有人可以帮助我吗? tks阅读!

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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM