html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

采用反射实现后期绑定操作EXCEL的简单代码(建议加入精华区)_.NET教程_编程技术

[ ] 已经帮助:人解决问题
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());
    }
}
ZykHTML5中文学习网 - HTML5先行者学习网
ZykHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助