Author: China Forum Network collection Source: http: //www.51one.net Joined: October 25, 2004
There are more articles on the Internet how to operate the Excel table, but all start the Excel window to open the Excel data file. Sometimes I need to embrace the Excel table into the FORM of my programs, which gives the customer an operation interface that does not switches the window, it seems better. This is easy to implement in the VC, but it is different in the C # China method. He will be explained below.
First, first briefly review how to operate the Excel table
First add a reference to Excel. Select Project -> Add References -> COM-> Add Microsoft Excel 9.0. (Different Office will have different versions of DLL files). Using Excel; Using System.Reflection; // Generate an Excel.Application App = New Excel.Application App = New Excel.Application (); if (App == Null) {Statusbar1.Text = "Error: Excel COULDN' ' Be Started! "; Return;} app.visible = true; // If you just use the program to control the Excel without wanting the user to operate, you can set to false app.userControl = true; Workbooks Workbooks = App.Workbooks; _Workbook Workbook = Workbooks.add (xlwbatemplate.xlwbatworksheet); // Generate new workbook // _Workbook workbook = workbooks.add ("c: //a.xls") according to the template; // or open the workbook file A according to the absolute path. XLS
Sheets Sheets = Workbook.worksheet; _Worksheet Worksheet = (_Worksheet) Sheets.get_Item (1); if (Worksheet == Null) {statusbar1.text = "Error: Worksheet == Null"; Return;}
// this Paragraph Puts The Value 5 To The Cell G1 Range Range1 = Worksheet.Get_Range ("A1", MISSING.VALUE); if (Range1 == Null) {statusbar1.text = "error: Range == null"; return Const int ncells = 2345; Range1.Value2 = ncells; Second, embed the Excel user interface into its own Windows Form
Because C # and VB.NET do not support OLE technology (see Microsoft Support Center INFO: 304562), this feature is only used to complete this feature using WebBrowser controls. (See Microsoft Support Center HOWTO: 304662) 1. Right-click Toolbox, select Custom Toolbox, add COM components, select "Microsoft Web Browser" (corresponding file is /winnt/system32/shdocvw.dll), determine . The text will appear in the toolbox will appear WebBroser control icons for Explorer. 2, add a WebBrowser control in Form1. (The object name is the province is AxWebBrowser1) 3. Assume that the Excel file to be opened is: c: /a.xls. string strFileName = @ "c: /a.xls"; Object refmissing = System.Reflection.Missing.Value; axWebBrowser1.Navigate (strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing); It is noted that with WebBrowser control The menu merge is not supported, that is, it is impossible to bring the menu of the Excel table into our program. This is a major disadvantage relative to the OLE implementation method. Fortunately, you can make many Excel proprietary operations with the toolbar you can add by the toolbar. // the following sentence can excel add in tune tool itself axWebBrowser1.ExecWB (SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref refmissing, ref refmissing); Third, back to the question presented in this paper, how to operate the embedded Excel? First, you need to understand that using the WebBrowser "load" Excel "table, actually run Excel.exe in the new process space. You can observe the task manager. Therefore, as long as we can get an Excel.Application object, you can get an Excel.Application object as mentioned in the above operation as an Excel data. Fortunately, the event can be provided by a method of the WebBrowser NavigateComplete access parameter e Excel.Application. public void axWebBrowser1_NavigateComplete2 (object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) {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)
// Since the Excel file is opened, the OAPPLICATION here is actually Excel.Application Excel.Application EAPP = (Excel.Application) OAPPLICATION; // This allows you to operate Excel as described above. } IV, how to ensure that the Excel process is also exited when the Form is exit. (See Microsoft Help Center KB317109)
Since WebBrowser is just a browsing of Excel tables, and Excel runs in separate processes. So to ensure that all member variables for this Excel object EAPP and its corresponding member variables can ensure that the Excel process follows when Form is exit. This is especially important when a program needs to turn off the Excel table multiple times. Excel.Application OAPP; Excel.Workbook Obook; Excel.Workbook Obook; Excel.Worksheet Osheet; .............................., osheet; OBOOK.CLOSE (false); NAR (OBOOK); NAR (OBOOKS); OAPP.quit (); NAR (OAP);
Debug.writeline ("Sleeping ..."); system.threading.thread.sleep (5000); debug.writeline ("end excel");} private void Nar (Object O) {Try {system.Runtime.InterOpServices. Marshal.releaseComobject (o);} catch {} finally {o = null;}} After experimenting, I found that the AxWebBroswer1 must be destroyed in addition to the release of these variables, and the Excel process exited. Otherwise, even if AxWebbroser1 goes to Navigate empty content "About: blank", the Excel process still does not quit. Therefore, AxWebBroser1 should be closed, or call AxWebBrowser1.dispose (); if it is still not necessary to quit normally, only call garbage is recycled. Gc.collect ();