//Get the name of the First Sheet. using (OleDbConnection con = new OleDbConnection(conStr)) { using (OleDbCommand cmd = new OleDbCommand()) { cmd.Connection = con; con.Open(); DataTable dtExcel = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); sheetName = dtExcel.Rows[0]["TABLE_NAME"].ToString(); con.Close(); } }
using (OleDbConnection cn = new OleDbConnection(conStr)) { using (OleDbCommand cmd = new OleDbCommand()) { using (OleDbDataAdapter oda = new OleDbDataAdapter()) { DataTable dt = new DataTable(); cmd.CommandText = "SELECT * From [" + sheetName + "]"; cmd.Connection = cn; cn.Open(); oda.SelectCommand = cmd; oda.Fill(dt); cn.Close();
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
I will explain here how to import data from excel to datagridview in c# windows application.
Show below Code and excel formate.
Excel File
Result