Windows Forms: How to Open and Read Excel Files in C#

This posts show you how to Open and Read Excel Files using ExcelDataReader, ExcelDataReader.DataSet in C# Windows Forms Application.

Creating a new windows forms application project, then open your form designer. You can drag TextBox, Label, Button, Combobox and DataGridView from the visual studio toolbox to your winform.

Open and Read Excel Files in C#

How to open an Excel file in C#

You need to install ExcelDataReader, ExcelDataReader.DataSet from Manage Nuget Packages to your project.

- ExcelDataReader: It's a lightweight and fast library written in C# for reading Microsoft Excel files (2.0-2007).

- ExcelDataReader.DataSet: This is an extension for reading Microsoft Excel files into System.Data.DataSet.

Next, Add the click event handler to the Browse button allows you to select the excel file, then open and read data in excel file in c#.

private void BtnBrowse_Click(object sender, EventArgs e)
    using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx" })
        if (openFileDialog.ShowDialog() == DialogResult.OK)
            txtFilename.Text = openFileDialog.FileName;
            using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
                using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
                    DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
                        ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
                    tableCollection = result.Tables;
                    foreach (DataTable table in tableCollection)
                        cboSheet.Items.Add(table.TableName);//add sheet to combobox

Using ExcelReaderFactory.CreateReader method allows you to easily read excel 97-2003 or excel workbook in c#.

Finally, Add the SelectedIndexChanged event handler to the Combobox allows you to select the sheet name, then load data from table into the DataGridView.

DataTable dt = tableCollection[cboSheet.SelectedItem.ToString()];
dataGridView1.DataSource = dt;

Through the c# example above you've learned how to read and import excel file to DataGridView in C# using ExcelDataReader and ExcelDataReader.DataSet library.