读取 Excel 到Dataset中

Posted on 2007-04-06 14:26  太阳天子  阅读(158)  评论(0)    收藏  举报

string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + source + ";Extended Properties=Excel 8.0";
            string query = "Select * From [Sheet1$]";

            OleDbConnection conn = new OleDbConnection(ConnStr);
            OleDbDataAdapter ad = new OleDbDataAdapter(query,conn);
            DataSet ds = new DataSet();
            ad.Fill(ds,"Book1");
            DataGrid1.DataSource = ds.Tables["Book1"].DefaultView;
            DataGrid1.DataBind();
            ad.Dispose();
            conn.Close();
            conn=null;

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3