您的位置:首页 > 其它

导入excle到DataTable

2013-06-06 22:32 127 查看
public static System.Data.DataTable InputExcel(string Path, ref string exceptionMsg)
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] sheet = new string[sheetDt.Rows.Count];
for (int i = 0; i < sheetDt.Rows.Count; i++)
{
sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString();
}
string strExcel = string.Format("select * from [{0}]", sheet[0]);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
dt = new System.Data.DataTable();
myCommand.Fill(dt);
conn.Close();
}
}
catch (Exception ex)
{
exceptionMsg = ex.Message;
}
return dt;
}


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