title: User select which excel sheet to be loaded in C#
body: <p>I want to read an <strong>excel</strong> file. This excel file have multiple <strong>sheets</strong>. The user have the choice to select which <strong>sheet</strong> is loaded in c# to work with it. I want to copy data into access database.</p>
  <p>I have written the code for loading excel workbook and for copying data into access database table. But in this code only user select the Excel file not the worksheet, i have hard coded the worksheet name <strong><code>Seven</code></strong> in code.</p>
  <blockquote>
  <p><strong>How to give functionality for user to select his own choice sheet in
  workbook excel. Also i want to retrieve all worksheets in excel file.</strong></p>
  </blockquote>
  <p>Code is under</p>
  <pre><code>private void btnImport_Click(object sender, EventArgs e)
      {
          var openFileExcel = new OpenFileDialog()
          {
              Filter = &quot;Excel Files | *.xlsx; *.xls; *.xlsm&quot;,
              Title = &quot;Select an Excel File&quot;,
              CheckFileExists = true
          };
          if (openFileExcel.ShowDialog() == DialogResult.Cancel)
              return;
          DatabaseObjects.FileName = openFileExcel.FileName;
          using(OleDbConnection conn = new OleDbConnection(DatabaseObjects.ConnectionString), 
              connExcel = new OleDbConnection(DatabaseObjects.ConnectionStringExcel))
          {
              string query = &quot;INSERT INTO Students (RollNo, SName, FName, ClassID) VALUES(@RollNo, @SName, @FName, @ClassID)&quot;;
              string queryExcel = &quot;SELECT * FROM [Seven$]&quot;;
              using (OleDbCommand command = new OleDbCommand(query, conn), commandExcel = new OleDbCommand(queryExcel,connExcel))
              {
                  OleDbParameter param1 = new OleDbParameter(&quot;RollNo&quot;, OleDbType.Numeric);
                  command.Parameters.Add(param1);
                  OleDbParameter param2 = new OleDbParameter(&quot;SName&quot;, OleDbType.VarChar);
                  command.Parameters.Add(param2);
                  OleDbParameter param3 = new OleDbParameter(&quot;FName&quot;, OleDbType.VarChar);
                  command.Parameters.Add(param3);
                  OleDbParameter param4 = new OleDbParameter(&quot;ClassID&quot;, OleDbType.Numeric);
                  command.Parameters.Add(param4);
                  conn.Open();
                  connExcel.Open();
                  OleDbDataReader drExcel = commandExcel.ExecuteReader();
                  while(drExcel.Read())
                  {
                      param1.Value = Convert.ToInt32(drExcel[0]);
                      param2.Value = drExcel[1].ToString();
                      param3.Value = drExcel[2].ToString();
                      param4.Value = Convert.ToInt32(drExcel[4]);
                      command.ExecuteNonQuery();
                  }
              }
          }
      }
  </code></pre>