随风而行

^o^ 格言:相信没有做不到的事情,只有想不到的事情.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

将Excel中的数据绑定到DataGrid控件中

Posted on 2009-02-24 14:23  随风而行  阅读(230)  评论(0编辑  收藏  举报
 
try
{
        DataSet   ds=new   DataSet();  
        string   strCon,strCmm;  
        strCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source =D:/test.xls;Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
        strCmm="select   distinct   *   from [Sheet1$]"; //Excel表中左下脚的表明要与[]中一致,绑定时必须关闭相应的Excel表
        OleDbConnection   oleCnn=new   OleDbConnection(strCon);  
        OleDbCommand   oleCmm=new   OleDbCommand(strCmm,oleCnn);  
        OleDbDataAdapter   oleDa=new   OleDbDataAdapter(oleCmm);  
        oleDa.Fill(ds,"test");  
        DataGrid1.DataSource=ds;
        DataGrid1.DataBind();
}
catch(Exception err)
{
        Label1.Text="出错:"+err.Message;
}