phpword通过浏览器下载文件

时间:2015-10-15 12:08:54

标签: json yii phpword

我正在尝试下载此文档,但我遇到了一些问题。我可以创建并保存它,但尝试在浏览器上输出时会弹出错误。

The file xxx.docx cannot be opened because there are problems with the contents.

接着是

Word found unreadable content in xxx.docx. Do you want to recover the contents of this document? 

我点击是,然后点击

this file cannot be opened using Microsoft Word.

我点击打开它没有问题就打开了。如果我浏览到存储文件的位置并打开它,它打开正常,所以我相信我的标题设置错误了吗?

// Saving the document as OOXML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename.".docx");
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $filename . '.docx"');
        //header("Content-Type: application/docx");
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header("Cache-Control: public");
        header('Expires: 0');
        $objWriter->save("php://output");

以下适用于Excel。这是一个json请求

$filename = "InvoiceSummaryReport_".date("Y-m", strtotime($datestr)) . "_" . date('Y-m-d_H-i-s_T').".xls";
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        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, 'Excel5');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename);

        echo "reports/$filename";

尝试复制单词,但它不起作用

$filename = "Weekly-MC-Report-" . date('d_F_Y').".docx" ;
        // Saving the document as OOXML file...
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename);      
        echo "reports/$filename";

1 个答案:

答案 0 :(得分:2)

我的工作定义(下面)唯一不同的是缓存控制部分,即您可以尝试使用这些(至少为了识别引起您头痛的行,即使您尝试对生成的内容进行缓存也是如此)文档)...

    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $file . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
    $objWriter->save('php://output');
相关问题