1读取数据 
读取Excel表格中的数据和读取数据库中的数据是非常类似的,在某种程度上Excel表格可以看成是一张一张的数据表。二者的主要区别在于所使用的数据引擎不一样。可通过下列代码实现读取Excel表格数据:

        //创建一个数据链接
        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 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 ( ) ) ;
}
}


4总结:

   以上只是读取了Excel表格中"Sheet1"中的数据,对于其他"Sheet"中的内容,可以参照读取"Sheet1"中的程序,只作一点修改就可以了,譬如要读取"Sheet2"中的内容,只需要把"Read.cs"程序中的"Sheet1$"改成"Sheet2$"就可以了。

posted on 2006-06-01 09:51  lanneret  阅读(3874)  评论(0编辑  收藏  举报