直接贴上代码:
public DataSet ExecleDs(string filenameurl)
{
DataSet ds = new DataSet();
//不同版本的Excel连接字符串不同
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filenameurl + ";Extended Properties=‘Excel 12.0; HDR=Yes; IMEX=1‘";
//string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filenameurl + ";Extended Properties=‘Excel 8.0;HDR=YES;IMEX=1;‘";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt!=null&&dt.Rows.Count >0)
{
foreach (DataRow item in dt.Rows)
{
string tableName =item[2].ToString().Trim();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + tableName + "]", conn);
odda.Fill(ds, tableName);
}
}
return ds;
}
代码太难看了,想贴张图的,无奈不知道怎么把图片放上去,就这样吧。。。。。。