PHP xlsx 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10198524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:37:04  来源:igfitidea点击:

PHP xlsx header

phpheaderxlsx

提问by GTCrais

so this works:

所以这有效:

myphpfile.php:

myphpfile.php:

<?php
header('Content-disposition: attachment; filename=myfile.pdf');
header('Content-type: application/pdf');
readfile('myfile.pdf');
?> 

that php file is called here and the PDF download works fine:

该 php 文件在此处调用,PDF 下载工作正常:

<a class = "oglasavanje" href = "../../cjenik/myphpfile.php">download</a><br/>

but this doesn't work:

但这不起作用:

<?php
header('Content-disposition: attachment; filename=myfile.xlsx');
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
readfile('myfile.xlsx');
?>

both .pdf and .xlsx files are in the same folder. When I click on the 'download' link the download windows pops up, I save the file, but the file can't be opened. It gives me the following error:

.pdf 和 .xlsx 文件都在同一个文件夹中。当我单击“下载”链接时,会弹出下载窗口,我保存了文件,但无法打开文件。它给了我以下错误:

Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

Excel 无法打开文件“myfile.xlsx”,因为文件格式或文件扩展名无效。验证文件未损坏并且文件扩展名与文件格式匹配。

The file opens fine when I open it manually (i.e. without downloading it via the link)

当我手动打开文件时,文件打开正常(即不通过链接下载)

I'm using WAMP, if that's of importance.

如果这很重要,我正在使用 WAMP。

Any help is appreciated.

任何帮助表示赞赏。

--- EDIT ---

- - 编辑 - -

I've found this piece of code on php.net:

我在 php.net 上找到了这段代码:

ob_clean();
flush();

so the final code looks like this and it works:

所以最终的代码看起来像这样,它可以工作:

$file = "myfile.xlsx";
header('Content-disposition: attachment; filename='.$file);
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush(); 
readfile($file);

Thanks everyone for the answers.

谢谢大家的回答。

回答by Baba

Depends on your browsers so many things can cause such behavior such as Content-length, Cache-Controletc

取决于你的浏览器,因此很多事情可能会导致这样的行为,如Content-lengthCache-Control

Also Change

也改变

Content-typeto Content-Type

Content-typeContent-Type

Content-dispositionto Content-Disposition

Content-dispositionContent-Disposition

Try

尝试

$file = "myfile.xlsx" ;
header('Content-Disposition: attachment; filename=' . $file );
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile('myfile.xlsx');

回答by Przemys?aw Sienkiewicz

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($filename);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Length: ' . filesize($filename));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile($filename);
return;

This solution works for me, but when i'm trying to open downloaded file i'm getting an error. For .pdf file is just empty.

此解决方案对我有用,但是当我尝试打开下载的文件时出现错误。对于 .pdf 文件只是空的。

回答by Scryptronic

Depending on your web browser, the filename may not be saving fully with periods. If the filename is incomplete, you can try and replace this header:

根据您的 Web 浏览器,文件名可能不会完全包含句点。如果文件名不完整,您可以尝试替换此标题:

header('Content-disposition: attachment; filename=myfile.xlsx');

with

$filename = "myfile.xlsx";
header('Content-Disposition: attachment; filename="' . $filename . '"');