1 /// <summary> 2 /// 读取指定路径的Excel内容到DataTable中 3 /// </summary> 4 /// <param name="path"></param> 5 /// <returns></returns> 6 public DataTable ImportToDataSet(string path) 7 { 8 string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + path + ";" + "Extended Properties=‘Excel 12.0;HDR=Yes;IMEX=1‘;"; 9 OleDbConnection conn = new OleDbConnection(strConn); 10 try 11 { 12 DataTable dt = new DataTable(); 13 if (conn.State != ConnectionState.Open) 14 conn.Open(); 15 string strExcel = "select * from [Sheet1$]"; 16 OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn); 17 adapter.Fill(dt); 18 return dt; 19 } 20 catch (Exception ex) 21 { 22 throw new Exception(ex.Message); 23 } 24 finally 25 { 26 if (conn.State != ConnectionState.Closed) 27 conn.Close(); 28 } 29 }
时间: 2024-10-13 00:57:41