111,126
社区成员
发帖
与我相关
我的任务
分享
string strConn = string.Empty;
if (filePath.Substring(filePath.LastIndexOf('.')).Equals(".xls"))
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
}
else
{
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
}
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
string sql = "SELECT * FROM [" + tableName + "]";//[Sheet1$]";//可更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet ds = new DataSet();
OleDaExcel.Fill(ds, tableName);
OleConn.Close();
return ds;