日期:2014-05-18 浏览次数:20909 次
public DataSet ExcelDS(string filenameurl,string tablename) { string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filenameurl + ";Extended Properties=\"Excel 12.0;HDR=YES\""; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); //這段Code的用意是在取得所有的SheetName DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "table" }); DataSet ds = new DataSet(); try { for (int i = 0; i < dt.Rows.Count; i++) { //這段Code是在檢查sheet的名字後面有沒有加上$,沒有的話,就補上. if (dt.Rows[i]["TABLE_Name"].ToString().IndexOf("$") < 0) { dt.Rows[i]["TABLE_Name"] += "$"; } OleDbDataAdapter odda = new OleDbDataAdapter("Select * From [" + dt.Rows[i]["TABLE_Name"].ToString() + "] ", conn); //读取第i个sheet odda.Fill(ds, tablename); } } catch (Exception ex) { throw; } return ds; }