501

I want excel files on a website to open in Excel when clicked, not get saved on desktop, or get opened embedded in a browser etc. Now obviously it all depends on how everything is configured for each user, but what's the best Content-Type and other settings to achieve just that most of the time?

taw
  • 16,697
  • 14
  • 52
  • 73
  • 11
    Although this is a duplicate, I think it should be kept around as a pointer to the linked duplicate. The titles are sufficiently different. – John Saunders Nov 06 '14 at 01:16
  • 2
    [Office 2007 File Format MIME Types for HTTP Content Streaming](https://blogs.msdn.microsoft.com/vsofficedeveloper/2008/05/08/office-2007-file-format-mime-types-for-http-content-streaming-2/) – KyleMit Jun 12 '18 at 20:23

3 Answers3

983

For BIFF .xls files

application/vnd.ms-excel

For Excel2007 and above .xlsx files

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Mark Baker
  • 199,760
  • 28
  • 325
  • 373
  • 2
    This does not work for me when using IE9. Chrome, Opera, Safari, Firefox - no problem. But IE fails to recognize the `Content-type` and I have checked with the debugger in IE to confirm that the Content-type is `application/vnd.ms-excel`. I have also tested lower/upper cased headers. Anyone experienced this? – luttkens Sep 24 '13 at 19:29
  • 1
    @luttkens - That'll be an issue with the combination of headers that you send, these are still the valid Content Types for Excel files – Mark Baker Sep 24 '13 at 19:30
  • 1
    @MarkBaker You're right. When I removed `Content-Disposition: attachment; filename='foo.xls'` it worked. But it doesn't really make me like IE... – luttkens Sep 24 '13 at 19:37
  • 19
    For anyone out there wondering: by removing the single-quotes around foo.xls from the `Content-Disposition` even IE will recognize how to open it. – luttkens Sep 24 '13 at 19:42
  • Hi, do you know the Content-Type of .xlsx files generated by Google Docs? I've tried your answer but no luck. – pikachu0 Mar 17 '14 at 09:55
  • I had an issue where I could not open this file type after downloading it programmatically. Basically it would save the file correctly, but when I did response.Transmitfile(..filepath..) where filepath was a legit filepath to a non-corrupted excel file, it would return a corrupted file. Changing response.ContentType from "plain/text" to "application/nvd.ms-excel" fixed it. – Lukas Mar 18 '14 at 20:58
  • 8
    .docx is worse application/vnd.openxmlformats-officedocument.wordprocessingml.document – mkz Jun 15 '14 at 14:59
  • what is content type for office 365 excel files ? – GANI Sep 23 '14 at 17:20
  • 7
    @user1016740 Office 365 files are OfficeOpenXML-format .xlsx files, so it's `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` – Mark Baker Sep 23 '14 at 17:52
  • (+1) With Mac system ,if you are getting additional (.xls) ,Then adding the second content type (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ) will fox it – Confused Jan 27 '16 at 07:05
  • @Confused - a ___second___ Content Type will simply overwrite the first, you can only have single content type header – Mark Baker Jan 27 '16 at 07:52
  • @MarkBaker I mean second line (i.e.application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) of the above answer, rather then first one(application/vnd.ms-excel). – Confused Jan 27 '16 at 12:15
  • Thanks for Nice answer BTW :-) – Confused Jan 27 '16 at 12:31
  • Why do they use such an ugly thing as this? – Hinrich Mar 14 '16 at 15:58
  • @Hinrich - because those ugly things tell the browser how to handle the data that you're sending it – Mark Baker Mar 14 '16 at 16:01
  • 9
    For those who are ignorant like me and weren't really sure what BIFF is, the following may help *"XLS is the file extension of the Microsoft Excel spreadsheet. It is created in the versions of Excel prior to 2007. It is a binary format based upon Binary Interchange File Format (BIFF) and thus it stores information in binary format."* – Steve Chambers Oct 31 '16 at 09:58
  • @MarkBaker, and how do you figured this out? – Black Nov 21 '19 at 09:00
42
application/vnd.ms-excel
miku
  • 161,705
  • 45
  • 286
  • 300
  • 3
    File formats listed above. For http content-type see [Office System File Format MIME Types](https://technet.microsoft.com/en-us/library/ee309278(office.12).aspx) – Nelda.techspiress May 26 '17 at 15:24
18

Do keep in mind that the file.getContentType could also output application/octet-stream instead of the required application/vnd.openxmlformats-officedocument.spreadsheetml.sheet when you try to upload the file that is already open.

CSchulz
  • 10,102
  • 9
  • 54
  • 107
DiTap
  • 311
  • 2
  • 5