MVC3 返回文件操作导致间歇性 Excel 程序错误
我有一个与此问题密切相关的问题 Microsoft Excel 错误:“向程序发送命令时出现问题。” 打开 Excel 会出现向程序发送命令时出错
错误。
但是,我使用的是 MVC3 以及生成一堆数据的操作,生成一个 excel 文件(使用< a href="http://npoi.codeplex.com/" rel="nofollow noreferrer">NPOI),将其写入 MemoryStream,然后使用内置 return 将其传输到浏览器File(etc)
ActionResult,与此类似(但此处缩短以提高可读性):
return File(myMemoryStream, "application/vnd.ms-excel", "filename.xls");
第一次单击触发此操作并返回此文件的链接时 - 它会出现错误。如果您按“确定”并重试,它会起作用,并且将继续起作用...永远
现在我知道这可能与禁用 DDE/插件 或 Excel 中的某些内容有关 - 但因为我正在生成一个 Excel 工作簿并将其转储到内存流,而不是打开文件系统上永久存在的内容,我不确定我必须采取哪些选项来消除该问题。
关于如何解决它有什么建议吗?也许我的哑剧类型不对?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Content-Type application/vnd.ms-excel 正在向浏览器发送命令以在浏览器中打开文件。这可能是问题的原因。尝试将内容类型设置为 application/x-msexcel。
在您的示例中,浏览器将尝试在浏览器中打开 Excel 电子表格(如果用户安装了 Excel)。
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).