采納反射完成后期綁定設(shè)置EXCEL的容易代碼(建議加入精華區(qū))
發(fā)表時(shí)間:2024-06-16 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]除了采用TblImp導(dǎo)入Excel object庫(kù)實(shí)現(xiàn)Excel的調(diào)用外.其實(shí)還可以采用反射的方法獲得屬性,并進(jìn)行后期綁定實(shí)現(xiàn)Excel的調(diào)用.下面是簡(jiǎn)單的調(diào)用EXCEL程序.using System;using System.Reflection;using System.Windows;usi...
除了采用TblImp導(dǎo)入Excel object庫(kù)實(shí)現(xiàn)Excel的調(diào)用外.
其實(shí)還可以采用反射的方法獲得屬性,并進(jìn)行后期綁定實(shí)現(xiàn)
Excel的調(diào)用.下面是簡(jiǎn)單的調(diào)用EXCEL程序.
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Forms;
class TestLateBound:System.Windows.Forms.Form
{
private Button myButton;
publicTestLateBound()
{
myButton=new Button();
myButton.Text="調(diào)用EXCEL";
myButton.Location=new System.Drawing.Point(100,100);
myButton.Click+=new System.EventHandler(TestBound);
this.Controls.Add(myButton);
this.Text="測(cè)試后期綁定 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++中差不多,需要將參數(shù)封裝為數(shù)組傳入
}
catch (Exception e)
{
MessageBox.Show (e.ToString());
}
}
public static void Main()
{
Application.Run(new TestLateBound());
}
}