gpt4 book ai didi

c# - Webbrowser Control 中的 Excel 从其他文件中锁定 Excel

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:03 25 4
gpt4 key购买 nike

描述
我正在将 Excel 文件加载到 WinForm 网络浏览器控件对象中。加载后,我可能需要打开其他 Excel 文件。但是当加载 webbrowser 控件时,它会阻止在使用双击时加载另一个工作簿。但是,如果我开始 -> 运行 Excel 应用程序,然后文件 -> 打开文档,它会加载,我可以在网络浏览器中的工作簿和我刚刚打开的工作簿之间进行交换。我正在使用 .NET 引用 Microsoft.Office.Internet.Excel 14.0.0.0。

在加载 webbrowser 控件时,是否可以通过双击打开文件?

在WebBrowser Control中加载Excel文件的代码

protected Microsoft.Office.Interop.Excel.Application _excelApp = null;  
protected Workbook _workbook = null;
protected Worksheet _worksheet = null;


//Load the Excel file in the WebBrowser control
this.webBrowser.Navigate("MyFile.xls", false);
//Get the ActiveXinstance from the WebBrowser Container
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)this.webBrowser.ActiveXInstance;
//Make the web browser silent so dialog boxes are not displayed
wb.Silent = true;
//Assign the document to an object
object odocument = wb.Document;

//Convert the Document to an Excel application
_excelApp = (Microsoft.Office.Interop.Excel.Application)odocument.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, odocument, null);

//Do not display alerts
_excelApp.DisplayAlerts = false;

//Assign a workbook to the Excel application. Use .Count in case there are other workbooks open in the Excel application
_workbook = (Workbook)_excelApp.Workbooks[_excelApp.Workbooks.Count];
//Assign a worksheet to the workbook (Excel objects are 1-based)
_worksheet = (Worksheet)_workbook.Worksheets[1];

我曾尝试在加载 webbrowser 控件后启动另一个 Excel 进程,但遇到了同样的问题:
System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\OFFICE11\Excel.exe");

我也曾尝试创建另一个 Excel 对象,但 .NET 引用似乎将两者合并为一个过程:
oXL=新的 Microsoft.Office.Interop.Excel.Application

我注意到这个 VS2008 问题被查看了很多但没有得到解决:https://stackoverflow.com/questions/2867744/webbrowser-locks-first-excel-instance

最佳答案

我找到了解决方法。您必须使用 Excel 实例的 IgnoreRemoteRequests 属性。但请记住 - 此设置是永久性的,这就是为什么您必须在关闭应用程序之前将其值设置回 False 的原因。一个方法,例如:

    public static void SetIgnoreRemoteRequests(WebBrowser bro, bool value)
{
if (bro.Document is Workbook)
{
((Workbook)bro.Document).Application.IgnoreRemoteRequests = value;
}
}

至于我,我在 OnLoadCompleted 中将其设置为 True,在 Navigating 中设置为 False (因为我的应用程序适用于许多其他格式)和 Disposing WebBrowser。

关于c# - Webbrowser Control 中的 Excel 从其他文件中锁定 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com