采用反射实现后期绑定操作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());
}
}