隐藏打开的Excel文件 [英] Hiding Opened Excel File

查看:86
本文介绍了隐藏打开的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

我开发了一个使用Microsoft.Excel COM组件的程序,以便读取/写入Excel文件。好吧,我的应用程序运行良好,但是例如,当我在程序运行时直接使用Excel打开另一个文件时,我的应用程序使用的文件会出现在Excel中。我不想要这个。我还尝试了Excel Application类的Visibility属性,但这不是解决方案,它不起作用。

I have developed a program that uses Microsoft.Excel COM components in order to read/write over Excel files. Well, my app is doing good but when I open, for instance, another file directly with Excel while my program is running, the file(s) that my app uses appear within Excel. I do not want this. I tried also the Visibility property of Excel Application class, but that was not the solution, it just does not work.

注意:我已经检查了这个问题。

NOTE : I have checked this question out.

限制访问由C#程序打开的excel文件

但是,它表示实际上没有适当的解决方案。

Yet, it says no proper solution actually.

推荐答案

您可以使用 Application.IgnoreRemoteRequests = true 。这样可以避免用户以与您使用的相同的 Excel 流程打开excel文件。

You can use Application.IgnoreRemoteRequests = true. This will avoid users opening excel files in the same Excel process as the one you are using.

只有一个但请注意:您必须确保应用程序的所有执行路径都将此属性重置为 false 。退出并释放 Excel 应用程序时,此属性不会会自行重置,这意味着 Excel 不会正确响应例如双击* .xls文件的后续用户。

There is one caveat though: you have to make sure that all execution paths of your application reset this property to false. This property WILL NOT reset itself when you quit and release your Excel application which means that Excel will not respond correctly to a subsequent user who double clicks on a *.xls file for example.

编辑:IgnoreRemoteRequest可能存在的问题

确定,我将详细说明使用此功能可能遇到的问题(至少这些是我遇到的唯一问题)

Ok, to make this clearer I'll detail a little bit more what issues you can run into using this feature (at least these are the only ones I've run into when I had to use this feature).

设置 IgnoreRemoteRequests = true 时,必须确保重置此设置属性,然后退出和/或释放COM Excel应用程序。如果您不这样做,Excel将不会响应DDE请求,这意味着如果有人双击* .xls文件,该文件将不会打开(Excel将启动,但不会自动打开该文件)。

When setting IgnoreRemoteRequests = true you have to make sure you reset this property BEFORE quiting and/or releasing the COM Excel application. If you don't, Excel will not respond to DDE requests which means if someone double clicks on a *.xls file, the file will not open (Excel will start up, but it wont open the file automatically).

但这仅在您退出应用程序和/或释放它而不重置属性的情况下才成立。您只需要确保要退出/重置的代码在任何位置,都可以将 IgnoreRemoteRquests 设置回 false 之前。

This however is only true if you quit the application and/or release it without reseting the property. You just have to make sure that wherever it is in your code that you are quitting/resetting you set the IgnoreRemoteRquests back to false before.

如果您的应用程序崩溃且无法清理(未处理的异常),那么EXCEL进程将继续运行(如果不可见,您只会在任务管理器中看到它)。这很正常,因为您的应用没有机会退出并释放其正在使用的内部Excel。但是,这不是问题。如果用户忽略此泄漏的Excel进程,直到它最终在下次重新启动或其他任何操作中被杀死,或者从任务栏手动将其杀死,则Excel会正常运行。

If you'r application crashes and it hasn't been able to clean up (unhandled exception) then the EXCEL process will keep running (if invisible, you will only see it in the Task Manager). That is normal as your app didnt have a chance to quit and release the internal Excel it is using. This however is not an issue. If a user ignores this "leaked" Excel process until it's eventually killed in next reboot or whatever, or manually kills it from the task bar, Excel will work perfectly fine.

注意:MS Excel2007。不知道以前版本的行为。

Note: MS Excel 2007. Don't know about behavior of previous versions.

这篇关于隐藏打开的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆