C # How to embed in Form and operate an Excel form

xiaoxiao2021-03-06  76

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 to 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 falseapp.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 workbook file A.xls according to the absolute path sheets sheets = workbook.Worksheets; _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 G1RANGE RANGE1 = Worksheet.get_Range ("A1", MISSING.VALUE); if (Range1 == Null) {statusbar1.text = "error: Range == null"; return;} const Int ncells = 2345 rge1.value2 = ncells; Second, embed an Excel user interface into its own Windows Form does not support OLE technology because C # and VB.NET (see Microsoft Support Center Info: 304562), so only use WebBrowser controls Complete this feature. (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, optin, null); // Since the OAPLICATION here is actually Excel.ApplicationExcel. Application EAPP = (Excel.Application) OAPPLICATION; // This can be used to operate Excel as described above. Help Center KB317109) Since WebBrowser is just a browsing of the Excel table, 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.

转载请注明原文地址:https://www.9cbs.com/read-97872.html

New Post(0)