【问题标题】:Excel in Webbrowser Control locks Excel from other filesWebbrowser Control 中的 Excel 从其他文件中锁定 Excel
【发布时间】:2011-04-05 18:24:53
【问题描述】:

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

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

在 WebBrowser 控件中加载 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=new Microsoft.Office.Interop.Excel.Application

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

【问题讨论】:

    标签: c# winforms visual-studio-2010 excel browser


    【解决方案1】:

    我找到了解决方法。您必须使用 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多