C#将EXCEL的数据复制到DATATABLE中?
1个回答
2013-06-18
展开全部
/// <summary>
/// 由EXCEL转换成DataTable
/// </summary>
/// <param name="strpath">文件路径及文件名 </param>
/// <returns> </returns>
private DataTable XlsToDataTable(String strpath)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strpath + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
注意Sheet1$,这是工作表的名字.
/// 由EXCEL转换成DataTable
/// </summary>
/// <param name="strpath">文件路径及文件名 </param>
/// <returns> </returns>
private DataTable XlsToDataTable(String strpath)
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strpath + ";" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataTable dt = new DataTable();
myCommand.Fill(dt);
return dt;
}
注意Sheet1$,这是工作表的名字.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询