Step 1: Create new widows form application.

Step 2: Create windows form with button, label, combobox and datagridview as below.

Step 3: Write following code.

  1. using System;
  2. using System.Data;
  3. using System.Data.OleDb;
  4. using System.Windows.Forms;
  5. namespace ExcelDemo
  6. {
  7. public partial class Form1: Form
  8. {
  9. OleDbConnection OleDbcon;
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14. private void button1_Click(object sender, EventArgs e)
  15. {
  16. OpenFileDialog openFileDialog = new OpenFileDialog();
  17. openFileDialog.Filter = "Excel Files|*.xls;*.xlsx";
  18. openFileDialog.ShowDialog();
  19. if (!string.IsNullOrEmpty(openFileDialog.FileName))
  20. {
  21. OleDbcon = new OleDbConnection(@ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog.FileName + ";Extended Properties=Excel 12.0;");
  22. OleDbcon.Open();
  23. DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
  24. OleDbcon.Close();
  25. comboBox1.Items.Clear();
  26. for (int i = 0; i < dt.Rows.Count; i++)
  27. {
  28. String sheetName = dt.Rows[i]["TABLE_NAME"].ToString();
  29. sheetName = sheetName.Substring(0, sheetName.Length - 1);
  30. comboBox1.Items.Add(sheetName);
  31. }
  32. }
  33. }
  34. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  35. {
  36. OleDbDataAdapter oledbDa = new OleDbDataAdapter("Select * from [" + comboBox1.Text + "$]", OleDbcon);
  37. DataTable dt = new DataTable();
  38. oledbDa.Fill(dt);
  39. dataGridView1.DataSource = dt;
  40. }
  41. private void button2_Click(object sender, EventArgs e)
  42. {
  43. OpenFileDialog OpenFileDialog = new OpenFileDialog();
  44. OpenFileDialog.ShowDialog();
  45. string path = OpenFileDialog.FileName;
  46. }
  47. }
  48. }

Step 4: Run application. And click on browse button.

Step 5: From open file dialog select excel file. When file is selected, all sheet names in file are filled in combobox.

Step 6: Select any sheet name from combobox. Then data from that selected sheet is filled in datagridview. The first row of excel sheet is considered as column header.