111,077
社区成员




//内嵌打开Excel
private void OpenExcelInForm(string strFileName)
{
eApp = new Excel.Application();
pnCellRFrmLock.Visible = false;
//eApp = new Excel.Application();
//eApp_protect = new Excel.Application();
object refmissing = System.Reflection.Missing.Value;
this.axWebBrowser1.Navigate(strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
}
/// <summary>
/// 首先需要明白,用WebBrowser“装载”Excel"表,实际上仍然是在新的进程空间中运行Excel.exe。可以用任务管理器观察。因此,只要我们能够获取Excel.Application对象,就能像上文一中所说的那样操作Excel数据。
///幸好可以通过WebBrowser的方法NavigateComplete提供的事件参数e来访问Excel.Application。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
try
{//.OLECMDID_HIDETOOLBARS
object refmissing = System.Reflection.Missing.Value;
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref refmissing, ref refmissing);
}
catch { }
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);
string aaa = oName.ToString();
Excel.Workbook wbb = (Excel.Workbook)oDocument;
eApp = wbb.Application as Excel.ApplicationClass;//获取到eApp,可以像外部打开一样操作Excel了,支持VBA函数
workBook = eApp.Workbooks[1];
workSheet = workBook.Worksheets[1] as Excel.Worksheet;
//但是问题出在下面!!!!
Office.CommandBar oCellRCommandBar;
oCellRCommandBar = workBook.Application.CommandBars["cell"];
oCellRButton = (Office.CommandBarButton)oCellRCommandBar.Controls.Add(Office.MsoControlType.msoControlButton, oMissing, oMissing, oMissing, true);//就这句!调试就跳出,也不出错!
//但是这句在弹出操作Excel时正常,而且可以成功创建一个Excel右键菜单项!
// Set the caption and face ID.
oCellRButton.Caption = "New game1";
oCellRButton.FaceId = 1845;
}