C#将EXCEL的数据复制到DATATABLE中?

2025-01-01 04:50:35
推荐回答(1个)
回答1:

///


/// 由EXCEL转换成DataTable
///

/// 文件路径及文件名
///
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$,这是工作表的名字.