【问题标题】:Setting mime type for excel document为 Excel 文档设置 MIME 类型
【发布时间】:2010-11-01 17:23:31
【问题描述】:

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 的名称显示为默认名称。

【问题讨论】:

  • 更一般地说,找出 MS 自己认为正确类型的最佳方法是找到一个安装了最新版本的框,然后查看注册表中 HKCR/.xlsContent Type 值.
  • "application/vnd.ms-office" 是 XLS 文件的另一种 mime 类型。
  • application/vnd-xls 也适用于.xls 文件。
  • MIME 类型和相关 excel 文件扩展名的完整列表请参阅this 帖子。
  • 标准的excel MIME类型是:-application/vnd.ms-excel

标签: excel content-type mime


【解决方案1】:

我相信标准的MIME type for Excel 文件是application/vnd.ms-excel

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

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

【讨论】:

  • application/msexcel和application/vnd.ms-excel有什么区别?
  • 如果您想在浏览器中显示文档(如果支持),那么 Content-Disposition 需要是“内联”的。见stackoverflow.com/questions/1395151/…
  • 你应该避免在 HTTP 中使用 Content-Disposition,它有安全考虑。 Content-Disposition 仅适用于电子邮件。请参阅stackoverflow.com/questions/1012437 了解更多信息。
  • 请注意,对于 OpenXML Excel XLSX 文件格式,定义了不同的 Mime 类型,请参阅提供的链接中的完整列表。
  • @DzmitryLazerka 什么是内容处置的“安全”替代方案?按照您的链接,然后是该答案中的链接,然后是该文档中的链接,将我们带到这里:tools.ietf.org/html/rfc6266#section-4.3。据我所知,安全问题只涉及用户提供的文件名——如果它们包含非拉丁字符,或者在某些操作系统上无效的字符。所有主要浏览器都有针对这些问题的保护措施。 Windows 和 Mac 还会在文件上设置一个标志,表明它来自互联网,当您尝试打开它时会弹出警告。
【解决方案2】:

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

application/vnd.ms-excel

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

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

【讨论】:

    【解决方案3】:

    我正在使用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);
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

      vnd.openxmlformats-officedocument.spreadsheetml.sheet
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          根据http://filext.com/file-extension/XLSX,.xlsx 的扩展名是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet。在检查 mime 类型时包含它可能是个好主意!

          【讨论】:

          • 这是我一直在寻找的,谢谢,需要支持最新格式以及以前的格式。
          猜你喜欢
          • 1970-01-01
          • 2015-09-17
          • 2015-06-16
          • 2011-07-04
          • 2013-04-21
          • 2015-03-01
          • 1970-01-01
          相关资源
          最近更新 更多