采用反射實現后期綁定操作EXCEL的簡單代碼(建議加入精華區)
2024-07-21 02:25:21
供稿:網友
除了采用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());
}
}