为excel文档设置mime类型

Sub*_*ian 340 excel mime content-type

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)

是否有任何一种适用于所有版本的类型?如果没有,我们是否需要response.setContentType()单独设置这些mime类型中的每一个?

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

jbo*_*chi 334

我相信Excel文件的标准MIME类型application/vnd.ms-excel.

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

header('Content-Disposition: attachment; filename="name_of_excel_file.xls"');
Run Code Online (Sandbox Code Playgroud)

  • application/msexcel和application/vnd.ms-excel有什么区别? (15认同)
  • 如果您想在浏览器中显示文档(如果支持),则Content-Disposition需要为"inline".请参阅http://stackoverflow.com/questions/1395151/content-dispositionwhat-are-the-differences-between-inline-and-attachment (5认同)
  • 您应该避免在HTTP中使用Content-Disposition,它有安全考虑因素.Content-Disposition仅适用于电子邮件.有关详细信息,请参阅http://stackoverflow.com/questions/1012437. (2认同)

Kar*_*ode 159

我在这里唤醒了一个旧线程,但我觉得有必要添加"新".xlsx格式.

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

  • 这就是我正在寻找的感谢,需要支持最新的格式以及以前的格式。 (2认同)

Siv*_*mar 38

如果要以xlsx格式提供excel文件,则应始终使用以下MIME类型

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
Run Code Online (Sandbox Code Playgroud)


Div*_*ria 6

我从.NET代码设置MIME类型如下 -

File(generatedFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
Run Code Online (Sandbox Code Playgroud)

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

vnd.openxmlformats-officedocument.spreadsheetml.sheet
Run Code Online (Sandbox Code Playgroud)


Dul*_*sta 6

For .xls use the following content-type

application/vnd.ms-excel
Run Code Online (Sandbox Code Playgroud)

For Excel 2007 version and above .xlsx files format

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Run Code Online (Sandbox Code Playgroud)