【问题标题】:PHPExcel can't save the file or download itPHPExcel 无法保存或下载文件
【发布时间】:2013-05-29 10:18:29
【问题描述】:

我正在尝试在 PHP 中编写一个函数来将一些数据导出到 Excel 文件。 问题是如果我将它保存到服务器它确实可以工作,但如果我尝试使用 php://output 发送到浏览器它就不起作用。它甚至不显示下载窗口。我收到了这些回复:

PK����a�B%���a��������[Content_Types].xml͔]K�0���%��f� "�v��R����kX����׿�m��+����4�a������tr5^�=Bb�9+c����,��9��.T"�kXr/�J,���[.��`ck6�?h�\� �,������}3�c�C+��9�-E��|c�j�BKPN�+�d��u��O1� o��Ba +����G�

标题是:

Response Headers
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Disposition attachment;filename="Report.xlsx"
Content-Type    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date    Wed, 29 May 2013 10:08:10 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=15, max=78
Last-Modified   Wed, 29 May 2013 10:08:11 GMT
Pragma  no-cache
Server  Apache/2.2.14 (Ubuntu)
Transfer-Encoding   chunked
X-Powered-By    PHP/5.3.2-1ubuntu4.19
Request Headers
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cookie  PHPSESSID=075r4aaqrvcnbca5sshjvm0jq7; 87293ba76812d31889be0901b086dd73=5h4jriq5c7r9vdt3m2u2u9up43; d82d00149fafbe651c9ba75a9804bbc9=en-GB
Host    150.145.139.3:8889
Referer 
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With    XMLHttpRequest

这是我的代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);



date_default_timezone_set('Europe/Rome');

require_once 'Classes/PHPExcel.php'; 
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';



$target ='templates/';
$fileType = 'Excel2007';   
$InputFileName = $target.'richiesta.xlsx';   
$OutputFileName = $target     .'Richiesta_'.$_SESSION['User'].'_'.$_SESSION['Last'].'_'.$dat.'.xlsx';



//Read the file (including chart template) 
$objReader = PHPExcel_IOFactory::createReader($fileType); 
//$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($InputFileName); 

 //Change the file 


$objPHPExcel->setActiveSheetIndex(0)
// Add data
            ->setCellValue('C3','10' )
            ->setCellValue('C4','20' )
            ->setCellValue('C5','30')
            ->setCellValue('C5','40' );


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);

//$objWriter->save($OutputFileName);  //This one WORKS FINE!!!




header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Report.xlsx"');


$objWriter->save('php://output'); //NOT WORKING :-(
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);

exit;

一旦我必须在本周完成这个项目,我就会对这个问题感到沮丧。 我非常感谢任何形式的帮助!

【问题讨论】:

    标签: php jquery phpexcel


    【解决方案1】:

    这只是来自我的代码,作为提示,问题似乎与Content-Type HTTP 标头有关:

    if (strtolower($type) == 'excel2003') {
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $outFileName . '"');
        header('Cache-Control: max-age=0');
    } else {
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
        header('Content-Type: application/xlsx');
        header('Content-Disposition: attachment;filename="' . $outFileName . '"');
        header('Cache-Control: max-age=0');
    }
    

    【讨论】:

    • 我更改了类型但没有成功...我已经完成了 stackoverflow 中的每一个问题/解决方案,但找不到解决方案帮助!
    • 转到您的浏览器设置,看看浏览器应该如何处理这些 MIME 类型,可能它被配置为显示而不是启动 Excel?
    • 不显示也不下载。在 Firefox 中我已经设置保存这种类型的文件...
    • 嗯,奇怪。错误日志上是否有任何错误(apache?) Tr$objWriter->save('php://output'); 的 inseatd 执行类似echo "hello world"; 的操作
    • header(...) 函数最常见的问题是输出缓冲区已经被刷新,然后header(...) 导致错误并且无法工作。你检查了吗?
    【解决方案2】:

    尝试使用这些 header() 调用:

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=Report.xlsx");
    header("Content-Transfer-Encoding: binary ");
    

    【讨论】:

      【解决方案3】:
       $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
       $filename = '1111';    
       ob_end_clean(); 
       header("Content-Type: application/vnd.openxmlformats- 
       officedocument.spreadsheetml.sheet");
       header('Content-Disposition:attachment;filename="'.$filename.'.xlsx"');
       header("Cache-Control: max-age=0");
       $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
       $writer->save('php://output');
       exit();
      

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人可以从中学习
      【解决方案4】:

      我遇到了同样的问题,似乎必须在不同的选项卡中打开下载,而不是在同一个选项卡中下载。这意味着您需要在初始化下载之前重定向到新选项卡。

      【讨论】:

      • @Vikrant 这是一个晦涩难懂的答案,但看起来像是在尝试回答这个问题。
      • 我宁愿说,这是对问题的评论
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2016-09-07
      • 2015-02-27
      相关资源
      最近更新 更多