【发布时间】: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