Avatar of Stefan Lennerbrant
Stefan Lennerbrant
Flag for Sweden

asked on 

Opening web-located text files in MS Excel

I generate text files in a web application, and I would like the client (mainly IE11 running on Windows 7 and 8) to open the files in Excel.

I serve the files as:
Content-Type: application/vnd.ms-excel
Content-Disposition: filename="myfile.txt"

Open in new window

and the content of the files is something like:
abc <tab> def <tab> ghi
123 <tab> 456 <tab> <789>

Open in new window

I have also tried using filename="myfile.htm" with a html content: <table><tr><td>...several rows/cells...</td></tr></table>


I run into several problems with this.
The biggest one is that this only works on small files. As soon as the file is larger than "quite small", WIndows and/or the browser "re-classifies" the file and renames it to myfile.xls (obviously(?) due to the mime type used). And then the Excel extension hardening kicks in and will issue a warning to the user. Not good!

In Chrome, there is a completely different problem. The file will always be downloaded to disk and will "loose" the connection to its mime type. The filename (extension) is never changed, as happens with IE, but saved on disk is is "only a text file" and will open in Notepad or similar, and not in Excel.
In IE, I get the same problem (of course) if the user chooses to save the file on disk, instead of opening it. In addition, IE sometimes renames the file to myfile.xls - which adds to the confusion :-)


So, in short - what is best practice?
How do I generate web content in non-binary form (tab-separated fields, comma-separated fields, html, or other text formats) that a modern browsers will open in MS Excel?
(the same question actually relates to MS Word, as well)

Any ideas?

/Stefan
Web DevelopmentMicrosoft ExcelWeb BrowsersPHP.NET Programming

Avatar of undefined
Last Comment
Stefan Lennerbrant

8/22/2022 - Mon