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 C#.NET binds Office

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to bind C#.NET Office, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

C#.NET binding Office late binding

Unlike earlier bindings, C#.NET binding Office late bindings wait until run time to bind property and method calls to their objects. To do this, the target object must implement a special COM interface: IDispatch. With the IDispatch::GetIDsOfNames method, Visual C # can ask which methods and properties the object supports, and then the IDispatch::Invoke method allows Visual C # to call these methods and properties. The advantage of this late binding is that it eliminates some of the version dependencies inherent in early binding. However, it also has the following disadvantages: it omits compile-time checking for automated code integrity, and does not provide "IntelliSense" functionality (which provides hints to help you invoke methods and properties correctly).

To use C#.NET binding Office late binding in Visual C #, use the System.Type.InvokeMember method. This method calls IDispatch::GetIDsOfNames and IDispatch::Invoke to bind to the methods and properties of the automation server.

Create an automated client that uses late binding

Start Microsoft Visual Studio .NET. On the File menu, click New, and then click Project. Select the Windows application from the Visual C# project type. A Form1 is created by default.

On the view menu, select the toolbox to display it, and then add a button to Form1.

Double-click Button1. The code window for the form appears.

In the code window, put the following code:

Replace private void button1_Click (object sender, System.EventArgs e) {} with: private void button1_Click (object sender, System.EventArgs e) {object objApp_Late; object objBook_Late; object objBooks_Late; object objSheets_Late; object objSheet_Late; object objRange_Late; object [] Parameters; try {/ / Instantiate Excel. ObjApp_Late = (object) new Excel.Application (); / / Get the workbooks collection. ObjBooks_Late = objApp_Late.GetType () InvokeMember ("Workbooks", BindingFlags.GetProperty, null, objApp_Late, null); / / Add a new workbook. ObjBook_Late = objBooks_Late.GetType () InvokeMember ("Add", BindingFlags.InvokeMethod, null, objBooks_Late, null); / / Get the worksheets collection. ObjSheets_Late = objBook_Late.GetType () InvokeMember ("Worksheets", BindingFlags.GetProperty, null, objBook_Late, null); / / Get the first worksheet. Parameters = new Object [1]; Parameters [0] = 1; objSheet_Late = objSheets_Late.GetType (). InvokeMember ("Item", BindingFlags.GetProperty, null, objSheets_Late, Parameters); / / Get a range object that contains cell A1. Parameters = new Object [2]; Parameters [0] = "A1"; Parameters [1] = Missing.Value; objRange_Late = objSheet_Late.GetType (). InvokeMember ("Range", BindingFlags.GetProperty, null, objSheet_Late, Parameters); / / Write "Hello, World!" In cell A1. Parameters = new Object [1]; Parameters [0] = "Hello, World!"; objRange_Late.GetType () .InvokeMember ("Value", BindingFlags.SetProperty, null, objRange_Late, Parameters); / / Return control of Excel to the user. Parameters = new Object [1]; Parameters [0] = true; objApp_Late.GetType (). InvokeMember ("Visible", BindingFlags.SetProperty, null, objApp_Late, Parameters); objApp_Late.GetType (). InvokeMember ("UserControl", BindingFlags.SetProperty, null, objApp_Late, Parameters);} catch (Exception theException) {String errorMessage; errorMessage = "Error:"; errorMessage = String.Concat (errorMessage, theException.Message); errorMessage = String.Concat (errorMessage, "Line:") ErrorMessage = String.Concat (errorMessage, theException.Source); MessageBox.Show (errorMessage, "Error");}} Thank you for reading this article carefully. I hope the article "how to bind Office to C#.NET" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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