首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHPExcel无法保存或下载文件

PHPExcel无法保存或下载文件
EN

Stack Overflow用户
提问于 2013-05-29 18:18:30
回答 4查看 12.6K关注 0票数 15

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

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

标头为:

代码语言:javascript
复制
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

这是我的代码:

代码语言:javascript
复制
<?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;

一旦我不得不在这个星期完成这个项目,我就会对这个问题感到沮丧。我真的很感谢大家的帮助!

EN

回答 4

Stack Overflow用户

发布于 2013-05-29 19:36:23

这只是我的代码中的一个提示,问题似乎出在Content-Type HTTP头上:

代码语言:javascript
复制
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');
}
票数 1
EN

Stack Overflow用户

发布于 2020-01-11 05:07:56

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

代码语言:javascript
复制
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 ");
票数 0
EN

Stack Overflow用户

发布于 2021-08-05 10:07:17

代码语言:javascript
复制
 $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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16811282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档