PHPExcel无法在浏览器中保存文件

问题描述

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

PK����a�B%���a��������[Content_Types].xml͔]K�0���%��f� "�v��R���kX����׿�m��+����4�

这是我的代码

<?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 :-(

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)