Sometimes the client wants to get the data from the multiple sheets of Excel file and show that the data on the page uss C# in ASP.Net so this article explains how to do that.

Initially I need to create an Excel file to get the data, so there is a file named "MyExcel.xlsx" with 3 sheets with some data like as in the following.

Data of Employees in Employee Sheet with Employee ID and Name Columns.

employee sheet

Data of Students in Student Sheet with Roll Number and Name Columns.

student sheet

Data of Teachers in Teacher Sheet with Teacher ID and Name Columns.

teacher sheet

Now to get the data from the preceding Excel file, I need to work on a page as in the following:

  • Add the "FileUpload" Control to upload the file
  • Add 3 Gridviews to show the data of all 3 sheets
  • Add a button with click event
  • Write some code on button click event in the code file
To learn more about this, check the following procedure.

Step 1

Add a new "Website" named "Website1".

new website

Add some controls to the default page named "Defaut.aspx".
  • Add the "FileUpload" Control to upload the file
  • Add 3 Gridviews to show the data of all 3 sheets
  • Add a button with click event
    1. <asp: FileUpload ID = "FileUpload1"
    2. runat = "server" / > < asp: Button ID = "Button1"
    3. runat = "server"
    4. Text = "Load Excel"
    5. OnClick = "Button1_Click" / > < asp: GridView ID = "GridView1"
    6. runat = "server" > < /asp:GridView>
    7. <asp:GridView ID="GridView2" runat="server"></asp: GridView > < asp: GridView ID = "GridView3"
    8. runat = "server" > < /asp:GridView>
gridview

It will look as in the following page.

on page
Step 2
Add 2 namespaces to the top of the code file.
  1. using System.IO;
  2. using System.Data.OleDb;
  3. using System.Data;
  • "System.IO" is used for the "File" and "Path" classes to access the Excel file.
  • "System.Data.OleDb" is used for the "OleDbConnection" and "OleDbConnection" classes to connect with an Excel file.
  • "System.Data" is used for the "DataTable" class.
Note

Microsoft Excel is like a database and OleDb is used to connect with many kinds of databases.

Add the following code to the button Click event.
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. //if File is not selected then return
  4. if (Request.Files["FileUpload1"].ContentLength <= 0)
  5. {
  6. return;
  7. }
  8. //Get the file extension
  9. string fileExtension = Path.GetExtension(Request.Files["FileUpload1"].FileName);
  10. //If file is not in excel format then return
  11. if (fileExtension != ".xls" && fileExtension != ".xlsx")
  12. {
  13. return;
  14. }
  15. //Get the File name and create new path to save it on server
  16. string fileLocation = Server.MapPath("\\") + Request.Files["FileUpload1"].FileName;
  17. //if the File is exist on serevr then delete it
  18. if (File.Exists(fileLocation))
  19. {
  20. File.Delete(fileLocation);
  21. }
  22. //save the file lon the server before loading
  23. Request.Files["FileUpload1"].SaveAs(fileLocation);
  24. //Create the QueryString for differnt version of fexcel file
  25. string strConn = "";
  26. switch (fileExtension)
  27. {
  28. case ".xls":
  29. //Excel 1997-2003
  30. strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
  31. break;
  32. case ".xlsx":
  33. //Excel 2007-2010
  34. strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0 xml;HDR=Yes;IMEX=1\"";
  35. break;
  36. }
  37. //Get the sheets data and bind that data to the grids
  38. BindData(strConn);
  39. //Delete the excel file from the server
  40. File.Delete(fileLocation);
  41. }
Here is the "BindData()" method that will get the sheets data and bind that to the grids.
  1. private void BindData(string strConn)
    {
  2. OleDbConnection objConn = new OleDbConnection(strConn);
  3. objConn.Open();
  4. // Get the data table containg the schema guid.
  5. DataTable dt = null;
  6. dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  7. objConn.Close();
  8. if (dt.Rows.Count > 0)
    {
  9. int i = 0;
  10. // Bind the sheets to the Grids
  11. foreach(DataRow row in dt.Rows)
    {
  12. DataTable dt_sheet = null;
  13. dt_sheet = getSheetData(strConn, row["TABLE_NAME"].ToString());
  14. switch (i)
    {
  15. case 0:
  16. GridView1.DataSource = dt_sheet;
  17. GridView1.DataBind();
  18. break;
  19. case 1:
  20. GridView2.DataSource = dt_sheet;
  21. GridView2.DataBind();
  22. break;
  23. case 2:
  24. GridView3.DataSource = dt_sheet;
  25. GridView3.DataBind();
  26. break;
  27. }
  28. i++;
  29. }
  30. }
  31. }
Note

The row["TABLE_NAME"] column in the datatable stores the sheet's names sorted alphabetically.

Here is the "getSheetData ()" method that will get the sheet name and return the datatable.
  1. private DataTable getSheetData(string strConn, string sheet)
    {
  2. string query = "select * from [" + sheet + "]";
  3. OleDbConnection objConn;
  4. OleDbDataAdapter oleDA;
  5. DataTable dt = new DataTable();
  6. objConn = new OleDbConnection(strConn);
  7. objConn.Open();
  8. oleDA = new OleDbDataAdapter(query, objConn);
  9. oleDA.Fill(dt);
  10. objConn.Close();
  11. oleDA.Dispose();
  12. objConn.Dispose();
  13. return dt;
  14. }
Step 3

Run the page.

run the page

Select the Excel file that I have created first and click on the "Load Excel" Button. Here is the result.

load excel