【问题标题】:open with option while downloading the xls file adds .xlsx extension下载 xls 文件时使用选项打开会添加 .xlsx 扩展名
【发布时间】:2019-11-28 18:20:06
【问题描述】:

我正在开发一项功能,我正在向用户证明 excel 转储。当用户下载 excel 表时,它工作正常。当用户尝试使用 Microsoft Excel 打开时,它给我一个错误“Excel 无法打开文件,因为文件格式或扩展名无效”这仅在 Firefox 浏览器中发生,并且在文件名中自动添加 .xlsx 扩展名。

我用来创建 excel 转储的代码是

    header('Pragma: public');
    header("Expires: Mon, 12 Jul 2012 05:00:00 GMT");                // Date in the past
    header('Content-Disposition: attachment; filename="excel.xls"');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
    header('Cache-Control: pre-check=0, post-check=0, max-age=0'); 

   $header  = "Date\t";
   $header .= "Other Details Details\t";
   echo $header . "\n";
   echo "Date1\t";
   echo "Detail1\t\n";
   echo "Date2\t";
   echo "Detail2\t\n";

【问题讨论】:

  • .xlsx 文件只能在 Microsoft Excel 2007+ 中打开,您确定您尝试打开它的那些计算机具有该版本的 Excel?
  • 是的,我使用的是 Excel 2007,而且我尝试下载的格式是 .xls
  • 你找到根本原因和解决方案了吗?

标签: php excel


【解决方案1】:

我对以下(不匹配的)响应标头有这个确切的问题:

Content-Disposition: attachment; filename="excel.xls"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

indicates 的内容类型是一个 .xslx 文件,这显然会导致 Firefox 添加 .xslx 扩展名。 Excel 显示错误,因为文件扩展名(由 Firefox 附加)是 .xslx,而文件格式是 xls。

要解决此问题,请将内容类型更改为:

Content-Type: application/vnd.ms-excel

【讨论】:

    猜你喜欢
    • 2021-07-20
    • 2019-11-07
    • 2015-09-21
    • 1970-01-01
    • 2014-11-15
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多