"Excel发现不可读的内容";打开用PHPExcel制作的Excel文件时出现警告 [英] "Excel found unreadable content" warning when opening Excel files made with PHPExcel

查看:244
本文介绍了"Excel发现不可读的内容";打开用PHPExcel制作的Excel文件时出现警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

I'm trying to download Excel files (xlsx) using PHPExcel as follows.

require_once("../../phpExcel/Classes/PHPExcel.php");
require_once("../../phpExcel/Classes/PHPExcel/IOFactory.php");

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Tiny")
->setLastModifiedBy("Tiny")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

$objPHPExcel->setActiveSheetIndex(0);
$sheet=$objPHPExcel->getActiveSheet();
$sheet->setCellValue('A1', 'Hello');
$sheet->setCellValue('B2', 'world!');
$sheet->setCellValue('C1', 'Hello');
$sheet->setCellValue('D2', 'world!');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');

它可以工作,但是当我尝试打开Excel文件时,它会在一致对话框中要求确认.

It works but when I try to open the Excel file, it asks for a confirmation in a conform dialog.

Excel在"test.xlsx"中发现了不可读的内容.你想恢复吗 该工作簿的内容?如果您相信这个来源 工作簿中,单击是".

Excel found unreadable content in 'test.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.

我还尝试了如下更改标题

I also tried to change the header as follows

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=test.xlsx");
header("Content-Transfer-Encoding: binary");

它仍然要求相同的确认.请问我在做什么错了?

It still asks for the same confirmation. What am I doing wrong here, please?

推荐答案

如询问用户Tiny自己的评论所述:

As told by questioning user Tiny on his own comment:

最后,它奏效了.我只是在PHP脚本的末尾添加了exit(在 问题中第一个代码段的末尾).非常感谢 所有人都给我有用的提示.

Finally, it worked. I just added exit at the end of my PHP script (at the end of the first code snippet in the question). Thanks very much all of you for giving me useful hints.

针对此类问题提供一些建设性的提示:

To give some constructive additional tips on this kind of problems:

  • 一个很好的提示是,您可以在所有PHP脚本文件中省略结束标记?>,这样您就知道在其末尾没有发送任何附加的不可见空格.
  • 在错误配置的Web服务器上的UTF-8上也包含PHP脚本文件可能会在脚本开始时发送不希望的几个字节.在Notepad ++上打开PHP文件,检查它是否为UTF-8,然后将其转换为ANSI.如果可以,请检查您的webserver/php配置.
  • header调用之前,您可以检查标头是否已通过以下方式错误发送:

  • A good tip is that you can omit the closing tag ?> on all your PHP script files, that way you know that you're not sending any aditional invisible whitespace at the end of it.
  • Also enconding PHP script files on UTF-8 on wrongfully configured web server can send an undesirable couple bytes at the begining of the script. Open the PHP file on Notepad++, check if it's UTF-8, and in that case, convert it to ANSI. If that makes it work, check your webserver/php configuration.
  • Just before the header calls, you can check if headers has been wrongfully sent with:

if ( headers_sent() ) die("**Error: headers sent");

如果您不能阻止某些函数调用将不希望的字符串发送到浏览器,则可以在脚本的开头就擦除"所有字符串:

If you can't prevent that some function call sends undesirable strings to the browser, you can "erase" all of it using at the very beginning of your script:

ob_start();

,然后在第一个headers调用之前,使用:

and then, just before the first headers call, use:

ob_clean();

请注意,这样做会阻止您收到错误反馈.

Be careful that with doing so will prevent you for receiving error feedback.

最后,正如已经说过的那样,如果此后在脚本上的某些点之后无需执行任何操作,请调用exitdie.

And lastly, as already said, if nothing has to be executed afterwards some point on the script, call exit or die.

这篇关于"Excel发现不可读的内容";打开用PHPExcel制作的Excel文件时出现警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆