开发者

MVC3 return File action causes intermittent Excel program error

I have a problem that closely relates to this problem Microsoft Excel Error: "There was a problem sending the command to the program." whereby opening Excel gives There was an error sending a command to the program error.

However, rather than the file existing and being opened or shortcutted-to, I am using MVC3 with an action that generates a bunch of data, generates an excel file (using NPOI), writes it to a MemoryStream and then chucks that to the browser开发者_Go百科 using the built-in return File(etc) ActionResult, with something akin (but shortened here to aid readability) to this:

return File(myMemoryStream, "application/vnd.ms-excel", "filename.xls");

The first time you click the link which fires this action and returns this File - it comes up with the error. if you press ok and try it again it works, and will continue to work... forever

Now I know this is potentially something to do with disabling DDE/plug-ins or something in Excel - but since I'm generating an excel workbook and dumping it to a memory stream rather than opening something that exists permanently on the file system, I'm not sure what options I have to remove the issue.

Any suggestions on how to get around it? Perhaps I have the wrong mime-type?


The Content-Type application/vnd.ms-excel is sending the command to the Browser to open the file in the Browser. Which can be the cause of issue. Try setting the content type to application/x-msexcel.

In your example the browser will try to open an Excel spreadsheet in the browser (if the user has Excel installed).

return File(myMemoryStream, "application/vnd.ms-excel", "filename.xls")

Please make the following change

return File(myMemoryStream, "application/x-ms-excel", "filename.xls")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜