开发者

Excel in Webbrowser Control locks Excel from other files

Description

I am loading an Excel file into a WinForm webbrowser control object. After it is loaded, I may need to open other Excel files. But when the webbrowser control is loaded, it prevents another workbook from loading when using a double-click. However, if I Start->Run the Excel application, then File->Open the document, it loads and I can interchange between the workbook in the webbrowser and the workbook I just opened. I am using .NET reference Microsoft.Office.Internet.Excel 14.0.0.0.

Is there anyway to open the file with a double click while the webbrowser control is loaded?

Code for loading the Excel file in WebBrowser Control

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)odocum开发者_如何学运维ent.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];  

I have tried to start another Excel process after the webbrowser control is loaded but had the same problem:

System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\OFFICE11\Excel.exe");

I have also tried creating another Excel object but the .NET reference seems to combine both into one process: oXL=new Microsoft.Office.Interop.Excel.Application

I noticed this VS2008 problem was viewed a lot but was not resolved: https://stackoverflow.com/questions/2867744/webbrowser-locks-first-excel-instance


I have found a workaround. You must use an IgnoreRemoteRequests property of an Excel instance. But remember - this setting is permanent, thats why you must set it's value back to False before closing application. A method, for example:

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

As for me, I'm setting this to True in OnLoadCompleted, and to False in Navigating (because my apllication works with many other formats) and on Disposing WebBrowser.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜