Introduction

This article explains how to read an Excel file into a Data Set Using ASP.NET.
Step 1
First of all, open a new Excel Sheet and enter the information that you want to add.
Now start Visual Studio and create a Web Application.
Right-click on this application and add the Excel File to this application.
Step 2
Now you need to add a Drop Down List, a List Item, a Label, and a Grid View to your application.
  1. <asp:DropDownList ID="dropdown1" runat="server" OnSelectedIndexChanged="ddlSlno_SelectedIndexChanged"
  2. AutoPostBack="true" AppendDataBoundItems="True">
  3. <asp:ListItem Selected="True" Value="Choose">- Choose -</asp:ListItem>
  4. </asp:DropDownList>
  5. <asp:GridView ID="Grid1" runat="server">
  6. </asp:GridView>
  7. <asp:Label ID="lbl1" runat="server" />
Step 3
Now add this code in the selected index change of the drop-down list:
  1. GenerateExcelData(dropdown1.SelectedValue);
For the GenerateExcelData method you need to add this code:
  1. private void GenerateExcelData(string SlnoAbbreviation)
  2. {
  3. try
  4. {
  5. string read = System.IO.Path.GetFullPath(Server.MapPath("~/empdetail.xlsx"));
  6. if (Path.GetExtension(read) == ".xls")
  7. {
  8. x = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + read + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
  9. }
  10. else if (Path.GetExtension(read) == ".xlsx")
  11. {
  12. x = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + read + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
  13. }
  14. x.Open();
  15. OleDbCommand y = new OleDbCommand();
  16. OleDbDataAdapter z = new OleDbDataAdapter();
  17. DataSet dset = new DataSet();
  18. y.Connection = x;
  19. y.CommandType = CommandType.Text;
  20. y.CommandText = "SELECT distinct([Slno]) FROM [Sheet1$]";
  21. z = new OleDbDataAdapter(y);
  22. z.Fill(dset, "Slno");
  23. dropdown1.DataSource = dset.Tables["Slno"].DefaultView;
  24. if (!IsPostBack)
  25. {
  26. dropdown1.DataTextField = "Slno";
  27. dropdown1.DataValueField = "Slno";
  28. dropdown1.DataBind();
  29. }
  30. if (!String.IsNullOrEmpty(SlnoAbbreviation) && SlnoAbbreviation != "Choose")
  31. {
  32. y.CommandText = "SELECT [Slno], [EmpName], [Salaray], [Location]" +
  33. " FROM [Sheet1$] where [Slno]= @Slno_Abbreviation";
  34. y.Parameters.AddWithValue("@Slno_Abbreviation", SlnoAbbreviation);
  35. }
  36. else
  37. {
  38. y.CommandText = "SELECT [Slno],[EmpName],[Salary],[Location] FROM [Sheet1$]";
  39. }
  40. z = new OleDbDataAdapter(y);
  41. z.Fill(dset);
  42. Grid1.DataSource = dset.Tables[1].DefaultView;
  43. Grid1.DataBind();
  44. }
  45. catch (Exception ex)
  46. {
  47. lbl1.Text = ex.ToString();
  48. }
  49. finally
  50. {
  51. x.Close();
  52. }
  53. }
This code will check both types of files to determine whether it's a .xls file or a .xlsx file.
After that I provided the connection for the Excel Sheet, in the starting it will show all the data but after that, it will fetch the data according to a Serial Number provided by you.
Step 4
Its complete code will be like this:
  1. using System;
  2. using System.Data.OleDb;
  3. using System.Data;
  4. using System.IO;
  5. namespace ReadExcelInToDataSet
  6. {
  7. public partial class Default : System.Web.UI.Page
  8. {
  9. OleDbConnection x;
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. GenerateExcelData("Choose");
  15. }
  16. }
  17. protected void ddlSlno_SelectedIndexChanged(object sender, EventArgs e)
  18. {
  19. GenerateExcelData(dropdown1.SelectedValue);
  20. }
  21. private void GenerateExcelData(string SlnoAbbreviation)
  22. {
  23. try
  24. {
  25. string read = System.IO.Path.GetFullPath(Server.MapPath("~/empdetail.xlsx"));
  26. if (Path.GetExtension(read) == ".xls")
  27. {
  28. x = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + read + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
  29. }
  30. else if (Path.GetExtension(read) == ".xlsx")
  31. {
  32. x = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + read + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
  33. }
  34. x.Open();
  35. OleDbCommand y = new OleDbCommand();
  36. OleDbDataAdapter z = new OleDbDataAdapter();
  37. DataSet dset = new DataSet();
  38. y.Connection = x;
  39. y.CommandType = CommandType.Text;
  40. y.CommandText = "SELECT distinct([Slno]) FROM [Sheet1$]";
  41. z = new OleDbDataAdapter(y);
  42. z.Fill(dset, "Slno");
  43. dropdown1.DataSource = dset.Tables["Slno"].DefaultView;
  44. if (!IsPostBack)
  45. {
  46. dropdown1.DataTextField = "Slno";
  47. dropdown1.DataValueField = "Slno";
  48. dropdown1.DataBind();
  49. }
  50. if (!String.IsNullOrEmpty(SlnoAbbreviation) && SlnoAbbreviation != "Choose")
  51. {
  52. y.CommandText = "SELECT [Slno], [EmpName], [Salaray], [Location]" +
  53. " FROM [Sheet1$] where [Slno]= @Slno_Abbreviation";
  54. y.Parameters.AddWithValue("@Slno_Abbreviation", SlnoAbbreviation);
  55. }
  56. else
  57. {
  58. y.CommandText = "SELECT [Slno],[EmpName],[Salary],[Location] FROM [Sheet1$]";
  59. }
  60. z = new OleDbDataAdapter(y);
  61. z.Fill(dset);
  62. Grid1.DataSource = dset.Tables[1].DefaultView;
  63. Grid1.DataBind();
  64. }
  65. catch (Exception ex)
  66. {
  67. lbl1.Text = ex.ToString();
  68. }
  69. finally
  70. {
  71. x.Close();
  72. }
  73. }
  74. }
  75. }
Output
On page load, it will show all the data.
readexcel1.jpg
Now when we provide the specific Serial Number then it will show that data only.
readexcel2.jpg