---
title: "How to import excel data in dataGridView in c# winform."  
description: "How to import excel data in dataGridView in c# winform."  
author: "Anonymous User"  
published: 2016-01-07  
updated: 2016-01-07  
canonical: https://www.mindstick.com/forum/33845/how-to-import-excel-data-in-datagridview-in-c-sharp-winform  
category: "c#"  
tags: ["c#", "excel"]  
reading_time: 2 minutes  

---

# How to import excel data in dataGridView in c# winform.

I want to [Import Excel](https://www.mindstick.com/forum/23040/to-import-excel-sheet-into-sql-database) [data in DataGridView](https://www.mindstick.com/forum/33964/insert-data-in-datagridview-from-text-box-c-sharp) In c# winform. How to do this please help me.

## Replies

### Reply by Anonymous User

I will explain here how to [import](https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from [excel](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) to [datagridview](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) in c# windows application.\
Show below Code and excel formate.\

```
using System;using System.Windows.Forms;using System.IO;using System.Data;using System.Data.OleDb;namespace Import_Excel{    public partial class Form1 : Form    {        private string Excel03Con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";        private string Excel07Con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";        public Form1()        {            InitializeComponent();        }        private void btnSelect_Click(object sender, EventArgs e)        {            openFileDialog1.ShowDialog();        }private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e){    string filePath = openFileDialog1.FileName;    string extension = Path.GetExtension(filePath);    string header = rtobtnHeaderYes.Checked ? "YES" : "NO";    string conStr, sheetName;    conStr = string.Empty;    switch (extension)    {        case ".xls": //Excel 97-03            conStr = string.Format(Excel03Con, filePath, header);            break;        case ".xlsx": //Excel 07            conStr = string.Format(Excel07Con, filePath, header);            break;    }    //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();                               GridView1.DataSource = dt;            }        }    }}private void Form1_Load(object sender, EventArgs e){}    }}
```

**Excel File**

```

```

**Result**

```
s
```


---

Original Source: https://www.mindstick.com/forum/33845/how-to-import-excel-data-in-datagridview-in-c-sharp-winform

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
