Dec 31, 2013

Import Excel File To A DataGridView in C#

In a windows form application sometimes you may want to import a excel file to a data grid view. In this blog post Ill demonstrate to you guys to do that. First of all open a new project and name it as you want. Design the form simple as you can with a Text Field, Button and a Data Grid View like shown in the below image. 



I've named my project as "ImportExcelFileToDataGridViewinCsharp". I've added the Text Field, Button and a Data Grid View. Just make sure you have renamed the properties as below to follow some coding standard which I followed in the code. 
     * TextField Name as 'txtFilePath'
     * Button Name as 'btnLoad' and Text as 'Load Excel'
     * DataGridView Name as 'dgvExcelData'

We need another item to do this task. That is OpenFileDialog, which uses to select the relevant file from windows explorer. You can find it in the toolbox.




Now lets do the coding part. Actually what you should happen is when we click the button we need to open the "Open File Dialog" window to select the file. To do that double click the button you will get the button click event. The final code will looks like below.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace ImportExcelFileToDataGridViewinCsharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtFilePath.Text = ofd.FileName;
                loadExcelToDataGrid(ofd.FileName);
            }
        }

        private void loadExcelToDataGrid(string strFilePath)
        {
            string sheet = "Sheet1";
            String strConnectionString = @"Data Source=" + strFilePath + "; Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;";
            OleDbConnection con = new OleDbConnection(strConnectionString);
            con.Open();
            OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [" + sheet + "$]", con);
            OleDbDataAdapter daCSV = new OleDbDataAdapter();
            daCSV.SelectCommand = cmdSelect;
            DataSet ds = new DataSet();
            daCSV.Fill(ds);
            dgvExcelData.DataSource = ds.Tables[0];
            con.Close();
        }

    }
}

One main thing to be note in the above code. You can see in the private method "loadExcelToDataGridView" I have assigned the "Sheet1" to the string variable sheet. This is the name of the excel sheet which you are going to upload. Simply you can use this code and improve it to capture more sheets as well as giving a selection to the user to select the sheet. 

Sometimes if you run the project in a new version of visual studio you may get the below exception. That means you need to install the 2007 Office System Driver: Data Connectivity Components.
Download Source Code

Hope you got what you are looking for.


No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...