2015-10-15 250 views
1

我想下载这个文件,但我有一些问题。我可以创建并保存它,但在尝试在浏览器上输出时出现错误。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"; 

回答

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'); 
+0

更新的问题 – shorif2000