设置 excel 文档的 mime 类型

问题描述 投票:0回答:6

MS Excel 具有以下观察到的 MIME 类型:

  • application/vnd.ms-excel
    (官方)
  • application/msexcel
  • application/x-msexcel
  • application/x-ms-excel
  • application/x-excel
  • application/x-dos_ms_excel
  • application/xls
  • application/x-xls
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    (xlsx)

有没有一种类型适用于所有版本?如果不是,我们是否需要分别为这些 mime 类型中的每一种设置

response.setContentType()

此外,我们在应用程序中使用文件流来显示文档(不仅仅是 excel - 任何类型的文档)。这样做时,如果用户选择保存文件,我们如何保留文件名——目前,呈现文件的 servlet 的名称显示为默认名称。

excel content-type mime
6个回答
387
投票

我相信 Excel 文件的标准MIME 类型是

application/vnd.ms-excel

关于文档的名称,您应该在响应中设置以下标题:

header('Content-Disposition: attachment; filename="name_of_excel_file.xls"');

205
投票

我在这里看到了一个旧线程,但我有添加“新”.xlsx 格式的冲动。

根据 http://filext.com/file-extension/XLSX .xlsx 的扩展名是

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
。在检查 MIME 类型时包含它可能是个好主意!


54
投票

对于 .xls 使用以下内容类型

application/vnd.ms-excel

适用于Excel 2007版本及以上.xlsx文件格式

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

9
投票

我正在从 .NET 代码中设置 MIME 类型,如下所示 -

File(generatedFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

我的应用程序使用 OpenXML SDK 生成 excel。这种 MIME 类型有效 -

vnd.openxmlformats-officedocument.spreadsheetml.sheet

2
投票

我正在使用 EPPlus 生成 .xlsx(基于 OpenXML 格式)excel 文件。为了在电子邮件中将此 excel 文件作为附件发送,我使用以下 MIME 类型,它与 EPPlus 生成的文件一起工作正常,并在 ms-outlook 邮件客户端预览中正确打开。

string mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
System.Net.Mime.ContentType contentType = null;
if (mimeType?.Length > 0)
{
    contentType = new System.Net.Mime.ContentType(mimeType);
}

1
投票

对于在使用问题中列出的所有可能的 MIME 类型后仍然对此感到困惑的人:

我发现 iMac 也倾向于为 XLS Excel 文件抛出 MIME 类型的“text/xls”,希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.