public static System.Data.DataTable InputExcel(string Path, ref string exceptionMsg) { System.Data.DataTable dt = new System.Data.DataTable(); try { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; using (OleDbConnection conn = new OleDbConnection(strConn)) { conn.Open(); System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string[] sheet = new string[sheetDt.Rows.Count]; for (int i = 0; i < sheetDt.Rows.Count; i++) { sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString(); } string strExcel = string.Format("select * from [{0}]", sheet[0]); OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn); dt = new System.Data.DataTable(); myCommand.Fill(dt); conn.Close(); } } catch (Exception ex) { exceptionMsg = ex.Message; } return dt; }
以上转自网上