【问题标题】:PHP What is wrong with the MIME type to export Excel sheet?PHP 导出 Excel 工作表的 MIME 类型有什么问题?
【发布时间】:2013-06-29 23:20:54
【问题描述】:

此输出存在问题。当用户单击按钮时,它应该吐出一个 Excel 表。但是,对于某些用户,它会输出一个 HTML 页面。我觉得这与其他使用 IE7 而不是 IE10 的用户有关。但是,此功能应该适用于两者。

请帮忙。

如果您能发现错误,请告诉我:

$time = time();
$illegals = array('!', '*', "'", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]", "\"");
if(isset($_GET['ns_title'])) {$filename = str_replace($illegals,"",urldecode($_GET['ns_title'])).".xls"; } else { $filename = "myreport$time.xls"; }
header("Content-type: application/force-download"); 
header("Content-Transfer-Encoding: Binary");    
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.$filename.'"'); 
header("Expires: 0");

print "$headers\n$data";

【问题讨论】:

  • $headers$data 变量中有什么内容?
  • 这是通过 ssl 连接吗?

标签: php excel export mime


【解决方案1】:

试试这个官方的 MIME 类型application/vnd.ms-excel

【讨论】:

    【解决方案2】:

    您指定了两种类型的 Content-type's - 坚持使用 application/octet-stream 或提供实际的 Excel 内容类型(请参阅 Bastien 的回答)。

    其次,$headers$data 是什么?我们假设您正在使用那些认为他们设置标题的人 - 但header() 已经这样做了。

    如果是真实文件,您可以使用readfile($filename),或者简单地回显您想要开始浏览器下载的内容。

    【讨论】:

    • 我的猜测是 $headers 是一个包含一组列标题的 tsv 字符串,而 $data 是一个包含要写入的数据的多行 tsv 字符串
    • $headers 和 $data 变量是从会话页面调用的全局变量。我认为这些不会导致出口问题,但话又说回来,我在这里寻求帮助。
    • 我也试过用这个:header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    • @John - 除非您的数据是 OfficeOpenXML 格式的 .xlsx 文件,否则您不应该使用 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;如果您的数据是 BIFF 格式的 .xls 文件,则 application/vnd.ms-excel 是正确的 mime 类型;但我很确定你的数据不是
    • 跟进:浏览器对如何处理 XYZ 文件类型有自己的配置。您只能提供浏览器可以期望的文件类型 - 它会处理它,但是它想要/配置为标题中提供的类型。
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2016-09-10
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2023-03-14
    • 2011-04-19
    相关资源
    最近更新 更多