您的位置:首页 > 其它

求教。将Excel文件导入DataTable中,然后绑定到GridView中。

2012-10-22 22:02 429 查看
private System.Data.DataTable GetExcelTable(string uploadPath)

{

DataSet ds = new DataSet();

string Xls_ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + uploadPath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';";//HDR为yes 则第一数据行为列名,为no 则自动为列加列名F1 F2 F3

OleDbConnection Conn = new OleDbConnection(Xls_ConnStr);

try

{

Conn.Open();

string sql_str = "select * from [Sheet1$]";

OleDbDataAdapter da = new OleDbDataAdapter(sql_str, Conn);

da.Fill(ds, "excel_data");

Conn.Close();

}

catch

{

if (Conn.State == ConnectionState.Open)

{

Conn.Close();

}

return null;

}

finally

{

Conn.Dispose();

}

if (ds == null)

{

return null;

}

if (ds.Tables.Count < 1)

{

return null;

}

return ds.Tables[0];

}

dt=GetExcelTable(ooxx);

DataGridView1.DataSource=dt.DefaultView;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: