日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
C#.NET綁定Office簡單介紹

C#.NET綁定Office晚期綁定

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

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

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

啟動 Microsoft Visual Studio .NET。在文件菜單上,單擊新建,然后單擊項目。從 Visual C# 項目類型中選擇 Windows 應用程序。默認情況下會創(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. }  

網站標題:C#.NET綁定Office簡單介紹
鏈接地址:http://www.dlmjj.cn/article/codepce.html