62,073
社区成员
发帖
与我相关
我的任务
分享
public static DataTable InputExcel(string path,string fileExtension)
{
string connectString = "";
if (".xls".Equals(fileExtension.ToLower()))
{
connectString = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " + path + ";Extended Properties=Excel 8.0;HDR=YES;IMEX=1;";
}
else if (".xlsx".Equals(fileExtension.ToLower()))
{
//connectString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 14.0;HDR=YES;IMEX=1;'";
connectString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 14.0;HDR=YES;IMEX=1'";
}
else
{
throw new Exception("导入文件格式不正确!");
}
try
{
OleDbConnection odc = new OleDbConnection(connectString);
OleDbCommand odcmd = new OleDbCommand("SELECT * FROM [Sheet1$]", odc);
DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter(odcmd);
odda.Fill(ds);
DataTable dt = ds.Tables[0].DefaultView.Table;
odc.Close();
ds.Dispose();
return dt;
}
catch (Exception ex)
{
throw ex;
}
}
}