先附上相关链接:http://zhidao.baidu.com/link?url=HmuHIZ11oYl-TpbbJRDBG-fTuo4Fxy0LbafP6Fs8-jc2fBQOtAyvivIHecp643Yh6X5e3H3qIXTZRe8_MWygKq
/创建一个数据链接 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM [Sheet1$] " ; myConn.Open ( ) ; file://打开数据链接,得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://创建一个 DataSet对象 myDataSet = new DataSet ( ) ; file://得到自己的DataSet对象 myCommand.Fill ( myDataSet , "[Sheet1$]" ) ; file://关闭此数据链接 myConn.Close ( ) ; 怎么样读取Excel表格中的数据其实和读取数据库中的数据没有什么实质上的区别。 注释:这里读取的是C盘根目录下的"Sample.xls"文件。 (2).用DataGrid来显示得到的数据集: 在得到DataSet对象后,只需要通过下列二行代码,就可以把数据集用DataGrid显示出来了: DataGrid1.DataMember= "[Sheet1$]" ; DataGrid1.DataSource = myDataSet ; (3).用Visual C#读取Excel表格,并用DataGrid显示出来的程序代码(Read.cs)和程序运行的界面: 掌握了上面二点,水到渠成就可以得到以下代码: using System ; using System.Drawing ; using System.Collections ; using System.ComponentModel ; using System.Windows.Forms ; using System.Data ; using System.Data.OleDb ; public class Form1 : Form { private Button button1 ; private System.Data.DataSet myDataSet ; private DataGrid DataGrid1 ; private System.ComponentModel.Container components = null ; public Form1 ( ) { file://初始化窗体中的各个组件 InitializeComponent ( ) ; file://打开数据链接,得到数据集 GetConnect ( ) ; } file://清除程序中使用过的资源 protected override void Dispose ( bool disposing ) { if ( disposing ) { if ( components != null ) { components.Dispose ( ) ; } } base.Dispose ( disposing ) ; } private void GetConnect ( ) { file://创建一个数据链接 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ; OleDbConnection myConn = new OleDbConnection ( strCon ) ; string strCom = " SELECT * FROM [Sheet1$] " ; myConn.Open ( ) ; file://打开数据链接,得到一个数据集 OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ; file://创建一个 DataSet对象 myDataSet = new DataSet ( ) ; file://得到自己的DataSet对象 myCommand.Fill ( myDataSet , "[Sheet1$]" ) ; file://关闭此数据链接 myConn.Close ( ) ; } private void InitializeComponent ( ) { DataGrid1 = new DataGrid ( ) ; button1 = new Button ( ) ; SuspendLayout ( ) ; DataGrid1.Name = "DataGrid1"; DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ; button1.Location = new System.Drawing.Point ( 124 , 240 ) ; button1.Name = "button1" ; button1.TabIndex = 1 ; button1.Text = "读取数据" ; button1.Size = new System.Drawing.Size (84 , 24 ) ; button1.Click += new System.EventHandler ( this.button1_Click ) ; this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ; this.Controls.Add ( button1 ) ; this.Controls.Add ( DataGrid1 ) ; this.Name = "Form1" ; this.Text = "读取Excle表格中的数据,并用DataGrid显示出来!" ; this.ResumeLayout ( false ) ; } private void button1_Click ( object sender , System.EventArgs e ) { DataGrid1.DataMember= "[Sheet1$]" ; DataGrid1.DataSource = myDataSet ; } static void Main ( ) { Application.Run ( new Form1 ( ) ) ; } } 下图是程序编译后,运行结果: http://www.yesky.com/20020313/jt-2002-3-13-image001.jpg 图01:用Visual C#读取"c:\sample.xls"的运行界面 (4).总结: 以上只是读取了Excel表格中"Sheet1"中的数据,对于其他"Sheet"中的内容,可以参照读取"Sheet1"中的程序,只作一点修改就可以了,譬如要读取"Sheet2"中的内容,只需要把"Read.cs"程序中的"Sheet1$"改成"Sheet2$"就可以了。 三.Visual C#调用Excel表格,并在Excel表格中存储数据: 在Visual C#中调用Excel表格,并不像读取Excel表格中的数据那么容易了,因为在Visual C#中调用Excel表格要使用到Excel的COM组件。如果你安装Office套件在"C"盘,那么在"C:\Program Files\Microsoft Office\Office"可以找到这个COM组件"EXCEL9.OLB",在《Visual C#如何使用Active X组件》一文中,这些COM组件都是非受管代码的,要在Visual C#中使用这些非受管代码的COM组件,就必须把他们转换成受管代码的类库。所以在用Visual C#调用Excel表格之前,必须完成从COM组件的非受管代码到受管代码的类库的转换。 (1).非受管代码COM组件转换成受管代码的类库: 首先把COM组件"EXCEL9.OLB"拷贝到C盘的根目录下,然后输入下列命令: tlbimp excel9.olb 这样在C盘的根目录下面就产生了三个DLL文件:"Excel.dll"、"Office.dll"、"VBIDE.dll"。在产生了上面的三个文件后,这种转换就成功完成了。在下面的程序中,就可以利用这转换好的三个类库编写和Excel表格相关的各种操作了。 (2).Visual C#打开Excel表格: 在"Excel.dll"中定义了一个命名空间"Excel",在差命名空间中封装了一个类"Application",这个类和启动Excel表格有非常重要的关系,在Visual C#中,只需要下列三行代码就可以完成打开Excel表格的工作,具体如下: Excel.Application excel = new Excel.Application ( ) ; excel.Application.Workbooks.Add ( true ) ; excel.Visible = true ; 但此时的Excel表格是一个空的表格,没有任何内容,下面就来介绍如何往Excel表格中输入数据。 (3).往Excel表格中输入数据: 在命名空间"Excel"中,还定义了一个类"Cell",这个类所代表的就是Excel表格中的一个下单元。通过给差"Cell"赋值,从而实现往Excel表格中输入相应的数据,下列代码功能是打开Excel表格,并且往表格输入一些数据。 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 ; (4). Visual C#调用Excel表格,并在Excel表格中存储数据的程序代码(Excel.cs): 了解了上面的这些知识,得到完成上述功能的程序代码就显得比较容易了,具体如下: 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 ; } }