2.在Form上增加一个控件来使其加载一个Excel文件,如下: Button的事件处理程序:private void button1_Click_1(object sender, EventArgs e){ string strFileName = @"d:\a.xls"; Object refmissing = System.Reflection.Missing.Value; this.webBrowser1.Navigate(strFileName); object axWebBrowser = this.webBrowser1.ActiveXInstance;}3.增加WebBrowser的DocumentComplete事件处理程序[C#] 复制 [保存] 保存private void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e){ Object refmissing = System.Reflection.Missing.Value; object[] args = new object[4]; args[0] = SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS; args[1] = SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER; args[2] = refmissing; args[3] = refmissing; object axWebBrowser = this.webBrowser1.ActiveXInstance; axWebBrowser.GetType().InvokeMember("ExecWB", BindingFlags.InvokeMethod, null, axWebBrowser, args); object oApplication = axWebBrowser.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, axWebBrowser, null); Excel.Workbook wbb = (Excel.Workbook) oApplication; Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass; Excel.Workbook wb = excel.Workbooks[1]; Excel.Worksheet ws = wb.Worksheets[1] as Excel.Worksheet; ws.Cells.Font.Name = "Verdana"; ws.Cells.Font.Size = 14; ws.Cells.Font.Bold = true; Excel.Range range = ws.Cells; Excel.Range oCell = range[10, 10] as Excel.Range; oCell.Value2 = "你好";}这里要注意,WebBrowser加载Excel文档后,并不显示工具栏,为此我们需要使用高级一点的技术,通过反射调用底层的方法来实现,首先我们需要找到WebBrowser封装的AxWebBrowser控件。[C#] 复制 [保存] 保存object axWebBrowser = this.webBrowser1.ActiveXInstance;然后,我们调用反射来显示工具栏[C#] 复制 [保存] 保存Object refmissing = System.Reflection.Missing.Value;object[] args = new object[4];args[0] = SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS;args[1] = SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER;args[2] = refmissing;args[3] = refmissing;axWebBrowser.GetType().InvokeMember("ExecWB", BindingFlags.InvokeMethod, null, axWebBrowser, args);
http://www.mscto.com
注:如果你直接使用AxWebBrowser控件,那你就可以直接调用ExecWB方法来显示工具栏。如下:[C#] 复制 [保存] 保存axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, refmissing, refmissing)好了,现在你需要获得你的Excel对象来操作Excel,此时需要使用Office的.Net库来完成。代码如下:[C#] 复制 [保存] 保存object oApplication = axWebBrowser.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, axWebBrowser, null);Excel.ApplicationClass excel = wbb.Application as Excel.ApplicationClass;Excel.Workbook wb = excel.Workbooks[1];Excel.Worksheet ws = wb.Worksheets[1] as Excel.Worksheet;ws.Cells.Font.Name = "Verdana";ws.Cells.Font.Size = 14;ws.Cells.Font.Bold = true;Excel.Range range = ws.Cells;Excel.Range oCell = range[10, 10] as Excel.Range;oCell.Value2 = "你好"; http://www.mscto.com 现在你可以随心所欲来控制Excel了,效果如下 说明:通过使用WebBrowser控件,是不能显示菜单栏的。 3.如果使用AxWebBrowser控件,那就需要处理NavigateComplete2事件,事件处理程序如下 private void axWebBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e){ Object refmissing = System.Reflection.Missing.Value; this.webBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref refmissing, ref refmissing); 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); 由于打开的是excel文件,所以这里的oApplication 其实就是Excel.Application Excel.Application excel = (Excel.Application) oApplication;//这样就可以象上文中所述来操作Excel了。 Excel.Workbook wb = excel.Workbooks[1]; Excel.Worksheet ws = wb.Worksheets[1] as Excel.Worksheet; ws.Cells.Font.Name = "Verdana"; ws.Cells.Font.Size = 14; ws.Cells.Font.Bold = true; Excel.Range range = ws.Cells; Excel.Range oCell = range[10, 10] as Excel.Range; oCell.Value2 = "你好"; http://www.mscto.com }这里操作更简单一点,缺点就是你必须直接使用AxWebBrowser控件,该控件比起已经封装好的WebBrowser可能有些我们所不知道的问题。微软推荐大家通过WebBrowser来访问。使用AxWebBrowser,有一个意外的收获,如下:[C#] 复制 [保存] 保存Object o = e.pDisp;IOleCommandTarget cmdTarget = o as IOleCommandTarget;Guid guidCMDGroup = new Guid("00020820-0000-0000-C000-000000000046");cmdTarget.Exec(ref guidCMDGroup, 1, 0, null, null);通过IoleCommandTarget可以模拟如菜单命令,这样你可以自己定制菜单,然后将菜单命令发送到Excel来处理。呵呵,模拟菜单。[C#] 复制 [保存] 保存public interface IOleCommandTarget{ [PreserveSig()] int QueryStatus([In, MarshalAs(UnmanagedType.Struct)] ref Guid pguidCmdGroup, [MarshalAs(UnmanagedType.U4)] int cCmds, [In, Out] IntPtr prgCmds, [In, Out] IntPtr pCmdText); 软件开发网 [PreserveSig()] int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdExecOpt, object[] pvaIn, [In, Out, MarshalAs(UnmanagedType.LPArray)] object[] pvaOut);}4.如果我们使用Visual C++我们可以获得一个完全的,嵌入式Excel,包括菜单,看起来就像我们自己的一样: 可惜我们对Visual C++都不太知道如何控制。