Setting mime type for excel document

ExcelContent TypeMime

Excel Problem Overview


MS Excel has the following observed MIME types:

  • application/vnd.ms-excel (official)
  • 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)

Is there any one type that would work for all versions? If not, do we need to set response.setContentType() with each one of these mime types individually?

Also, we use file streaming in our application to display document (not just excel - any type of document). In doing so, how can we retain the filename if the user opts to save the file - currently, the name of the servlet that renders the file appears as the default name.

Excel Solutions


Solution 1 - Excel

I believe the standard MIME type for Excel files is application/vnd.ms-excel.

Regarding the name of the document, you should set the following header in the response:

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

Solution 2 - Excel

Waking up an old thread here I see, but I felt the urge to add the "new" .xlsx format.

According to http://filext.com/file-extension/XLSX the extension for .xlsx is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. It might be a good idea to include it when checking for mime types!

Solution 3 - Excel

For .xls use the following content-type

application/vnd.ms-excel

For Excel 2007 version and above .xlsx files format

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Solution 4 - Excel

I was setting MIME type from .NET code as below -

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

My application generates excel using OpenXML SDK. This MIME type worked -

vnd.openxmlformats-officedocument.spreadsheetml.sheet

Solution 5 - Excel

I am using EPPlus to generate .xlsx (OpenXML format based) excel file. For sending this excel file as attachment in email I use the following MIME type and it works fine with EPPlus generated file and opens properly in ms-outlook mail client preview.

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);
}

Solution 6 - Excel

For anyone who is still stumbling with this after using all of the possible MIME types listed in the question:

I have found that iMacs tend to also throw a MIME type of "text/xls" for XLS Excel files, hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSubramanianView Question on Stackoverflow
Solution 1 - ExceljbochiView Answer on Stackoverflow
Solution 2 - ExcelKarlis RodeView Answer on Stackoverflow
Solution 3 - ExcelDu-LacosteView Answer on Stackoverflow
Solution 4 - ExcelDivyans MahansariaView Answer on Stackoverflow
Solution 5 - ExcelVishwajit GView Answer on Stackoverflow
Solution 6 - Excelbe_esView Answer on Stackoverflow