如何在使用 Microsoft.Office.Interop 打开文件时禁用弹出窗口
如只读确认、其他提醒等。 如何处理这些弹出窗口?还是忽略他们?
Such as read-only confirm, other alerts.
What to do with these popups? Or ignore them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅我的答案此处。
基本上,您可以通过“显示警报”方法禁用所有警报:
其中 [OFFICE_APP] 是您正在使用的 Office 程序的名称,例如 Word、Excel 等。
See my answer here.
Basically, you disable all alerts via the "Display Alerts" method:
where [OFFICE_APP] is the name of the Office program you're using, such as Word, Excel, etc.
这是防止安全消息要求您允许宏的另一种选择。
我读了 这篇文章来自MSDN并找出了以下代码:
由于在打开文件之前已创建文件的副本,因此我不必将AutomationSecurity更改回默认设置。
Here is another alternative to prevent the Security message asking you to allow macros.
I read this article from MSDN and figured out the following code:
Since a copy of the file is made before opening it I don't have to change the AutomationSecurity back to the default setting.
添加注释:对于某些受密码保护的文件格式(我测试了 .XLS,但可能还有其他格式),
app.DisplayAlerts = false
将不会绕过密码对话框。在这种情况下,您可以简单地在打开时传递一个假密码,这将引发错误。如果你愿意的话就抓住它吧。
在这种情况下抛出的错误是:
Adding a note: for some file formats (I tested .XLS, but probably others too) that are password protected,
app.DisplayAlerts = false
will NOT bypass the password dialog.In this situation, you can simply pass a fake password on open, which will throw an error. Catch it if you want.
In this situation the error thrown is:
亲爱的“Uriel Fernandez”和他的想法
https: //learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.automationsecurity?view=excel-pia
让我想到了另一个想法https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.screenupdating?view=excel-pia#Microsoft_Office_Interop_Excel__Application_ScreenUpdating
所以可以尝试禁用它< strong>_Application.ScreenUpdating 和...“您将无法看到代码在做什么,但它会运行得更快”
编辑
Dear "Uriel Fernandez" with his thought
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.automationsecurity?view=excel-pia
directed me to another thought https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._application.screenupdating?view=excel-pia#Microsoft_Office_Interop_Excel__Application_ScreenUpdating
So can try disabling it _Application.ScreenUpdating and ... "You won't be able to see what the code is doing, but it will run faster"
EDIT
试试这个:
这将禁用弹出窗口。
Try this:
This will disable the popups.
对于那些被诅咒得仍然需要使用 Excel Interop 的人来说。
可能有效的方法是使用 NetOffice.Excel nuget 包并设置以下内容:
如果您使用的是 excel 365,您仍然会收到隐私级别警告,并记住
DisplayAlerts = false;
不会禁用安全/隐私警告。要停止 365 上的隐私级别警告,您需要禁用电源查询上的隐私防火墙:
通过代码将“AutomationSecurity”设置为低应该可以做到这一点,但它对我不起作用。
对于有关外部数据的其他警告,您可以尝试在服务器的 Excel 配置上自行禁用它:
https://support.microsoft.com/en-gb/office/enable -或禁用安全警报关于来自可疑网站的链接和文件-a1ac6ae9-5c4a-4eb3-b3f8-143336039bbe
For anyone cursed enough to still have to work with Excel Interop.
What MIGHT work is using the NetOffice.Excel nuget package and setting the following:
If you're using excel 365 you'll still get privacy levels warnings, and remember that
DisplayAlerts = false;
doesn't disable security/privacy warnings.To stop privacy level warnings on 365 you need to disable privacy firewall on power query:
Setting the
'AutomationSecurity
' to low via code was supposed to do this, but it wouldn't work for me.For other warnings about external data, something you can try is disable it yourself on your server's excel configurations:
https://support.microsoft.com/en-gb/office/enable-or-disable-security-alerts-about-links-and-files-from-suspicious-websites-a1ac6ae9-5c4a-4eb3-b3f8-143336039bbe