|
using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Data.SqlClient ; public class Form1 : Form { private Button button1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { file://初始化窗体中的各个组件 InitializeComponent ( ) ; } file://清除程序中使用的各个资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose( disposing ) ; } private void InitializeComponent ( ) { button1 = new Button ( ) ; SuspendLayout ( ) ; button1.Location = new System.Drawing.Point ( 32 , 72 ) ; button1.Name = "button1" ; button1.Size = new System.Drawing.Size ( 100 , 30 ) ; button1.TabIndex = 0 ; button1.Text = "调用Excel文件!" ; button1.Click += new System.EventHandler ( button1_Click ) ;
AutoScaleBaseSize = new System.Drawing.Size ( 5 , 13 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ; this.Controls.Add ( button1 ) ; this.Name = "Form1" ; this.Text = "如何用Visual C#调用Excel表格!" ; this.ResumeLayout ( false ) ;
} static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } private void button1_Click ( object sender , System.EventArgs e ) { Excel.Application excel = new Excel.Application ( ) ; excel.Application.Workbooks.Add ( true ) ; excel.Cells[ 1 , 1 ] = "第一行第一列" ; excel.Cells[ 1 , 2 ] = "第一行第二列" ; excel.Cells[ 2 , 1 ] = "第二行第一列" ; excel.Cells[ 2 , 2 ] = "第二行第二列" ; excel.Cells[ 3 , 1 ] = "第三行第一列" ; excel.Cells[ 3 , 2 ] = "第三行第二列" ; excel.Visible = true ; } } |