php PHPExcel 生成的 excel 文件不起作用。(文件格式或扩展名无效)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16805851/
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
PHPExcel generated excel file not working. (File format or extension is invalid )
提问by sqlchild
PHPExcel generated excel file not working. (File format or extension is invalid)
PHPExcel 生成的 excel 文件不起作用。(文件格式或扩展名无效)
The example code of PHPExcel exceutes successfully on my local PC, the file gets downloaded to local and is opened in Excel 2007 successfully. But using the same code on my server gives an Invalid file to excel, which when I OPEN AND REPAIR, then it opens successfully.
PHPExcel的示例代码在我的本地PC上成功执行,文件下载到本地并在Excel 2007中成功打开。但是在我的服务器上使用相同的代码会为 excel 提供一个无效的文件,当我打开并修复时,它会成功打开。
I don't want to use the Open and Repair option, but only the Open option.
我不想使用打开和修复选项,而只想使用打开选项。
Couldn't find out the problem? Please help.
查不出问题?请帮忙。
$objPHPExcel->setActiveSheetIndex(0);
//for XLSX
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
回答by Govind
Add a line ob_end_clean();
at the end of your program before the line $objWriter->save('php://output');
ob_end_clean();
在该行之前的程序末尾添加一行$objWriter->save('php://output');
The code looks like: //for XLSX
代码如下: //for XLSX
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
exit;
回答by Mark Baker
The file that you've posted has a single space character before the PHPExcel output... check your script to see where this might be sent to the php://output stream. Check that there's no space before your initial <?php
opening tag; watch out in particular for ?> <?php
or similar closing/opening tags. And also check any files that might be included
by your script
您发布的文件在 PHPExcel 输出之前有一个空格字符...检查您的脚本以查看它可能发送到 php://output 流的位置。检查初始<?php
标签前是否没有空格;特别注意?> <?php
或类似的结束/开始标签。并检查included
您的脚本可能存在的任何文件
回答by Ian Atkin
Check for any spurious characters being echoed into the file by opening it in a text editor. If there are, you can probably figure out where they're being introduced. Spaces or line breaks are obvious culprits.
通过在文本编辑器中打开文件,检查是否有任何虚假字符回显到文件中。如果有,您可能会弄清楚它们是在哪里引入的。空格或换行是明显的罪魁祸首。
Also, make sure that there are no spaces or newlines before the <?php
tag or following the ?>
tag in any of your files.
此外,请确保在任何文件中<?php
的?>
标记之前或标记之后没有空格或换行符。
The xlsx
format is actually a zipped collection of xml
files, so if there are no obvious text strings (beside the PK signature) visible in a text editor, then try unzipping it. That may give other clues to the issue.
该xlsx
格式实际上是一个压缩xml
文件集合,因此如果在文本编辑器中没有明显的文本字符串(在 PK 签名旁边)可见,请尝试解压缩它。这可能会为该问题提供其他线索。
If you're running on a Windows platform, then there have been some buggy versions of the ZipArchive php_zip.dll
link library that can cause this error. You can use PCLZip as an alternative to ZipArchive.
如果您在 Windows 平台上运行,那么 ZipArchivephp_zip.dll
链接库的一些错误版本可能会导致此错误。您可以使用 PCLZip 作为 ZipArchive 的替代品。
回答by Rogerio de Moraes
IS application/vnd.ms-excel
是 application/vnd.ms-excel
AND NOT
并不是
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
EXAMPLE FOR XLS:
XLS 示例:
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
EXAMPLE FOR XLSX:
XLSX 示例:
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="item_list.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
回答by liyakat
Writing an Excel file isn't simply a matter of giving an arbitrary file an extension of xlsx or xls.
编写 Excel 文件不仅仅是为任意文件指定 xlsx 或 xls 扩展名的问题。
You can write a BIFF5 or BIFF8 xls file (all versions of Excel post 95 will read this), or there is the Excel 2003 XML format, which can be opened by both Excel 2003 and Excel2007 (and Excel 2010 as well).
您可以编写 BIFF5 或 BIFF8 xls 文件(Excel 95 后的所有版本都会读取此文件),或者有 Excel 2003 XML 格式,可以通过 Excel 2003 和 Excel2007(以及 Excel 2010)打开。
For more detail and example to do with excel 2007 you can refer to the PHPExcel examplespage.
有关 excel 2007 的更多详细信息和示例,您可以参考PHPExcel 示例页面。
Let me know if I can help you more.
如果我能帮助你更多,请告诉我。
回答by vivek
I faced same issue and found there was a new line character, which was coming from the beginning of the desired data and was causing this error. Later I found that I had a php file which had closing tag
我遇到了同样的问题,发现有一个换行符,它来自所需数据的开头并导致了这个错误。后来我发现我有一个带有结束标记的 php 文件
"?>"
Usually it is recommended that one should avoid using closing php tag as they also go to output buffer. Using ob_end_clean() is also good option.
通常建议避免使用关闭 php 标签,因为它们也会进入输出缓冲区。使用 ob_end_clean() 也是不错的选择。
More details can be found Why would one omit the close tag?
可以找到更多详细信息为什么会省略关闭标签?
回答by Suraj Kumar Maurya
I solved my problem, I installed zip using command
我解决了我的问题,我使用命令安装了 zip
sudo apt-get install php7.0-zipand Restart the server sudo service apache2 restartIt works for me I used https://github.com/PHPOffice/PhpSpreadsheet
sudo apt-get install php7.0-zip并重启服务器 sudo service apache2 restart它对我有用我用过https://github.com/PHPOffice/PhpSpreadsheet