日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

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

新聞中心

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

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. }  

文章題目:C#.NET綁定Office簡單介紹
本文來源:http://m.5511xx.com/article/codepce.html