Background
I have often read the common question in forum posts of how to upload Excel file records and bind them to a GridView but no one has provided the proper solution and many solutions contain a lot of code that is not required so by considering the preceding requirements I have decided to write this article to provide the solution to bind a GridView from Excel file records with a minimum amount of code. So let us start creating an application so beginners can also understand.
First create the Excel file named Employee as:
Now we have records to bind to the GridView, let us create the sample web application as follows:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New WebSite" - "C#" - "Empty WebSite" (to avoid adding a master page).
  3. Provide the web site a name such as "BindGridviewFromExcelFileRecords" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select "Add New Item" and Add Web Form.
  5. Drag and drop one Button, a GridView and a FileUploader control onto the <form> section of the Default.aspx page.

Now the default.aspx page source code will look such as follows.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>Article by Vithal Wadje</title>
  6. </head>
  7. <body bgcolor="blue">
  8. <form id="form1" runat="server">
  9. <div style="color: White;">
  10. <h4>
  11. Article for C#Corner
  12. </h4>
  13. <table>
  14. <tr>
  15. <td>
  16. Select File
  17. </td>
  18. <td>
  19. <asp:FileUpload ID="FileUpload1" runat="server" />
  20. </td>
  21. <td>
  22. </td>
  23. <td>
  24. <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
  25. </td>
  26. </tr>
  27. </table>
  28. </div>
  29. <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
  30. </form>
  31. </body>
  32. </html>
Now open the Default.aspx.cs page and write the following code to create an oledbconnection for the Excel file as in the following:
  1. private void ExcelConn(string FilePath)
  2. {
  3. constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
  4. Econ = new OleDbConnection(constr);
  5. }
Create a function to read the Excel File records and bind the GridView as:
  1. private void ReadExcelRecords(string FilePath)
  2. {
  3. ExcelConn(FilePath);
  4. Query = string.Format("Select [Name],[City],[Address],[Designation] FROM [{0}]", "Sheet1$");
  5. OleDbCommand Ecom = new OleDbCommand(Query, Econ);
  6. Econ.Open();
  7. DataSet ds = new DataSet();
  8. OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
  9. Econ.Close();
  10. oda.Fill(ds);
  11. GridView1.DataSource = ds;
    GridView1.DataBind();
  12. }
Now call the preceding function upon an Upload button click as:
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
  4. ReadExcelRecords(CurrentFilePath);
  5. }
The entire code of the default.aspx.cs page will look as follows:
  1. using System;
  2. using System.Data;
  3. using System.IO;
  4. using System.Data.OleDb;
  5. public partial class _Default : System.Web.UI.Page
  6. {
  7. OleDbConnection Econ;
  8. string constr, Query;
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. }
  12. private void ExcelConn(string FilePath)
  13. {
  14. constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", FilePath);
  15. Econ = new OleDbConnection(constr);
  16. }
  17. private void ReadExcelRecords(string FilePath)
  18. {
  19. ExcelConn(FilePath);
  20. Query = string.Format("Select [Name],[City],[Address],[Designation] FROM [{0}]", "Sheet1$");
  21. OleDbCommand Ecom = new OleDbCommand(Query, Econ);
  22. Econ.Open();
  23. DataSet ds = new DataSet();
  24. OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
  25. Econ.Close();
  26. oda.Fill(ds);
  27. GridView1.DataSource = ds;
    GridView1.DataBind();
  28. }
  29. protected void Button1_Click(object sender, EventArgs e)
  30. {
  31. string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
  32. ReadExcelRecords(CurrentFilePath);
  33. }
  34. }
Now run the application and the UI will look as follows:
Now select the file by using the browse button as:
Now click on the Upload button. The records in GridView will then look as in the following:
Now you have seen how the records are displayed in the GridView using an Excel file with a minimal amount of code and effort.
Notes
  • For detailed code please download the sample Zip file.

  • Do a proper validation such as date input values when implementing.
Summary

From all the preceding examples you have learned how to bind a GridView with Excel file records. I hope this article is useful for all readers, if you have a suggestion then please contact me.