採用反射實現後期綁定操作EXCEL的簡單代碼(建議加入精華區)
來源:互聯網
上載者:User
excel|精華 除了採用TblImp匯入Excel object庫實現Excel的調用外.
其實還可以採用反射的方法獲得屬性,並進行後期綁定實現
Excel的調用.下面是簡單的調用EXCEL程式.
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
class TestLateBound:System.Windows.Forms.Form
{
private Button myButton;
public TestLateBound()
{
myButton=new Button();
myButton.Text="調用EXCEL";
myButton.Location=new System.Drawing.Point(100,100);
myButton.Click+=new System.EventHandler(TestBound);
this.Controls.Add(myButton);
this.Text="測試後期綁定 Excel Application";
}
public void TestBound(object sender,System.EventArgs ef)
{
Type myExcel;
myExcel=Type.GetTypeFromProgID("Excel.Application");
object objExcel;
objExcel=Activator.CreateInstance(myExcel);
object[] param=new object[1];
param[0]=true;
try
{
myExcel.InvokeMember("Visible",BindingFlags.SetProperty,null,objExcel,param); //和VC++中差不多,需要將參數封裝為數組傳入
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
}
public static void Main()
{
Application.Run(new TestLateBound());
}
}