自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#.NET綁定Office簡單介紹

開發(fā) 后端
這里介紹要在 Visual C# 中使用C#.NET綁定Office晚期綁定,請使用 System.Type.InvokeMember 方法。

C#.NET綁定Office晚期綁定

與早期綁定不同,C#.NET綁定Office晚期綁定要等到運行時才會將屬性和方法調(diào)用綁定到它們的對象。為此,目標(biāo)對象必須實現(xiàn)一個特殊的 COM 接口:IDispatch。利用 IDispatch::GetIDsOfNames 方法,Visual C# 可以詢問對象支持哪些方法和屬性,然后,IDispatch::Invoke 方法允許 Visual C# 調(diào)用這些方法和屬性。這種晚期綁定的優(yōu)點是:它消除了早期綁定所固有的某些版本依賴性。然而,它也有以下缺點:省略了對自動化代碼完整性的編譯時檢查,也不提供“智能感知”功能(該功能可提供有助于正確調(diào)用方法和屬性的提示)。

要在 Visual C# 中使用C#.NET綁定Office晚期綁定,請使用 System.Type.InvokeMember 方法。此方法調(diào)用 IDispatch::GetIDsOfNames 和 IDispatch::Invoke 來綁定到自動化服務(wù)器的方法和屬性?!?/P>

創(chuàng)建使用晚期綁定的自動化客戶端

啟動 Microsoft Visual Studio .NET。在文件菜單上,單擊新建,然后單擊項目。從 Visual C# 項目類型中選擇 Windows 應(yīng)用程序。默認(rèn)情況下會創(chuàng)建 Form1。

在視圖菜單上,選擇工具箱以顯示工具箱,然后向 Form1 添加一個按鈕。

雙擊 Button1。將出現(xiàn)該窗體的代碼窗口。

在代碼窗口中,將以下代碼:

  1. private void button1_Click(object sender, System.EventArgs e)  
  2.  
  3. {  
  4. }  
  5. 替換為:private void button1_Click(object sender, System.EventArgs e)  
  6. {  
  7. object objApp_Late;  
  8. object objBook_Late;  
  9. object objBooks_Late;  
  10. object objSheets_Late;  
  11. object objSheet_Late;  
  12. object objRange_Late;  
  13. object[] Parameters;  
  14. try  
  15. {  
  16. // Instantiate Excel.  
  17. objApp_Late = (object)new Excel.Application();  
  18. //Get the workbooks collection.  
  19. objBooks_Late = objApp_Late.GetType().InvokeMember( "Workbooks",  
  20. BindingFlags.GetProperty, null, objApp_Late, null );  
  21. //Add a new workbook.  
  22. objBook_Late = objBooks_Late.GetType().InvokeMember( "Add",  
  23. BindingFlags.InvokeMethod, null, objBooks_Late, null );  
  24. //Get the worksheets collection.  
  25. objSheets_Late = objBook_Late.GetType().InvokeMember( "Worksheets",  
  26. BindingFlags.GetProperty, null, objBook_Late, null );  
  27. //Get the first worksheet.  
  28. Parameters = new Object[1];  
  29. Parameters[0] = 1;  
  30. objSheet_Late = objSheets_Late.GetType().InvokeMember( "Item",  
  31. BindingFlags.GetProperty, null, objSheets_Late, Parameters );  
  32. //Get a range object that contains cell A1.  
  33. Parameters = new Object[2];  
  34. Parameters[0] = "A1";  
  35. Parameters[1] = Missing.Value;  
  36. objRange_Late = objSheet_Late.GetType().InvokeMember( "Range",  
  37. BindingFlags.GetProperty, null, objSheet_Late, Parameters );  
  38. //Write "Hello, World!" in cell A1.  
  39. Parameters = new Object[1];  
  40. Parameters[0] = "Hello, World!";  
  41. objRange_Late.GetType().InvokeMember( "Value", BindingFlags.SetProperty,  
  42. null, objRange_Late, Parameters );  
  43. //Return control of Excel to the user.  
  44. Parameters = new Object[1];  
  45. Parameters[0] = true;  
  46. objApp_Late.GetType().InvokeMember( "Visible", BindingFlags.SetProperty,  
  47. null, objApp_Late, Parameters );  
  48. objApp_Late.GetType().InvokeMember( "UserControl", BindingFlags.SetProperty,  
  49. null, objApp_Late, Parameters );  
  50. }  
  51. catch( Exception theException )  
  52. {  
  53. String errorMessage;  
  54. errorMessage = "Error: ";  
  55. errorMessage = String.Concat( errorMessage, theException.Message );  
  56. errorMessage = String.Concat( errorMessage, " Line: " );  
  57. errorMessage = String.Concat( errorMessage, theException.Source );  
  58. MessageBox.Show( errorMessage, "Error" );  
  59. }  

【編輯推薦】

  1. C#字符串操作步驟
  2. C#集成開發(fā)環(huán)境淺析
  3. Visual C# .NET應(yīng)用程序
  4. C# TimeLabel控件詳解
  5. C#復(fù)合控件開發(fā)技術(shù)
責(zé)任編輯:佚名 來源: cnblogs
相關(guān)推薦

2009-08-24 16:19:54

C#.NET綁定Off

2009-09-11 11:30:53

Net60C#.NET

2009-08-31 14:45:15

C#.NET多線程應(yīng)用

2009-08-25 13:53:20

C#.NET rege

2009-08-26 14:23:14

C#.Net Fram

2011-06-17 15:55:19

ArrayListC#

2009-08-13 10:35:55

C#.NET操作XML

2009-08-26 10:09:22

C#編碼規(guī)范

2009-08-19 15:44:09

ObjectARX .

2009-08-20 18:44:54

C#和ADO.NET

2009-08-18 16:57:24

VB.NET和C#

2009-08-19 16:05:46

AutoCADEditor類

2009-04-02 15:21:43

c#IDisposeFinalize

2009-08-28 09:29:02

2009-07-28 14:06:28

ASP.NET 2.0

2009-10-14 13:15:09

VB.NET數(shù)據(jù)綁定

2009-08-19 16:19:33

Employee對象

2009-08-28 14:15:19

SocketVisual C#.N

2011-06-01 15:45:28

實體類序列化

2009-09-01 16:14:05

ArrayList與A
點贊
收藏

51CTO技術(shù)棧公眾號