Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simply embed and operate excel Table in C # form

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to simply embed and operate the excel table in C # form? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

When we implement the from function, we need to embed the excel table into the C # form of our program to give customers a simple interface that does not have to switch windows. The author will tell you how to achieve it in c # below.

First, add a reference to Excel. Select Project-> add reference-> COM- > add Microsoft Excel 9.0. Different office will have different versions of dll files.

Using Excel; using System.Reflection; / / generate a new process of Excel.Application Excel.Application app = new Excel.Application (); if (app = = null) {statusBar1.Text = "ERROR: EXCEL couldn't be started!"; return;} app.Visible = true; / / if you only want to use the program to control the excel and do not want the user to operate, you can set it to false app.UserControl = true Workbooks workbooks = app.Workbooks; _ Workbook workbook = workbooks.Add (XlWBATemplate.xlWBATWorksheet); / / generate a new workbook / / _ Workbook workbook = workbooks.Add ("c:\\ a.xls") according to the template; / / or open the workbook file a.xls Sheets sheets = workbook.Worksheets; _ Worksheet worksheet = (_ Worksheet) sheets.get_Item (1) according to the absolute path 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 your own C # form

Since neither C# nor vb.net currently supports OLE technology (see Microsoft support Center Info:304562), only WebBrowser controls can be used to accomplish this function. (see Microsoft support Center Howto:304662 for the following methods)

1. Right-click the toolbox, select Custom Toolbox, add COM components, select "Microsoft Web browser" (the corresponding file is\ winnt\ system32\ shdocvw.dll), and make sure. The WebBroser control icon with the text Explorer appears in the toolbox.

2. Add a WebBrowser control to C#form1. (the object name is saved as axWebBrowser1)

3. Suppose 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 worth noting that menu merging is not supported with WebBrowser controls, that is to say, menus from Excel tables cannot be brought into our program. This is a major disadvantage compared to the OLE implementation approach. Fortunately, it provides the ability to add toolbars, through which you can perform many Excel-specific operations.

/ / the following sentence can add the tool tone of excel itself.

AxWebBrowser1.ExecWB (SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS

SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,ref refmissing, ref refmissing)

Third, back to the question raised in this article, how to operate the embedded Excel?

First of all, you need to understand that loading the "Excel" table with WebBrowser is actually still running Excel.exe in the new process space. You can observe it with the task manager. Therefore, as long as we can get the Excel.Application object, we can manipulate the Excel data as mentioned in the first above.

Fortunately, Excel.Application can be accessed through the event parameter e provided by WebBrowser's method NavigateComplete.

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;// so that you can manipulate Excel as described above. }

4. When the C # form containing the webbrowser exits, how can I ensure that the excel process also exits? (see microsoft help Center kb317109)

Because WebBrowser is just browsing the Excel table, Excel runs in a separate process. So make sure that references are released to the Excel object eApp and all its corresponding member variables to ensure that the excel process exits when Form exits. This is especially important when a program needs to open and close the excel table multiple times.

Excel.Application oApp; Excel.Workbooks oBooks; Excel.Workbook oBook; Excel.Worksheet oSheet;. Private void ExcelExit () {NAR (oSheet); oBook.Close (False); NAR (oBook); NAR (oBooks); oApp.Quit (); NAR (oApp); 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 experimentation, I found that in addition to releasing these variables, the axWebBroswer1 must also be destroyed before the Excel process exits. Otherwise, even if axWebBroser1 is asked to Navigate the empty content "about:blank", the excel process will not quit. Therefore, the C # form where axWebBroser1 is located should be closed, or axWebBrowser1.Dispose should be called directly.

This is the answer to the question about how to simply embed and operate the excel table in C # form. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report