Windows Forms: How to Open and Read Excel Files in VB.NET

This post shows you How to Open and Read Excel Files using ExcelDataReader, ExcelDataReader.DataSet in VB.NET Windows Forms Application.

To open and read Excel files using ExcelDataReader in VB.NET, you need to follow these steps

How to read excel file in windows application

Open your Visual Studio, then create a new Windows Forms Application project, then design a simple UI that allows you to select an excel file, then open, read and import data from excel file to DataGridView in as shown below. excel to datagridview

How to Open and Read Excel Files in VB.NET

First, You'll need to install the ExcelDataReader package by right-clicking on your project, then select Manage Nuget Packages. Next, Search and install ExcelDataReader, ExcelDataReader.DataSet to your project.

You can do this via NuGet Package Manager or through the NuGet command-line interface. Here's the NuGet command to install it:

Install-Package ExcelDataReader
Install-Package ExcelDataReader.DataSet

ExcelDataReader is a popular library used for reading data from Excel files. It's particularly useful when you need to work with Excel files without relying on Microsoft Office or Excel itself.

ExcelDataReader is a lightweight written in C# for reading Microsoft Excel files (XLS, XLSX).

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

Here's a basic example of how to use ExcelDataReader to read data from an Excel file in VB.NET.

Add the click event handler to the Browse button that allows you to select the excel file, then read data from the excel file to DataTable and return as a DataSet.

' connect to excel file
' exceldatareader
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx"}
        If ofd.ShowDialog() = DialogResult.OK Then
            txtFileName.Text = ofd.FileName
            Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                Using reader As IExcelDataReader = ExcelReaderFactory.CreateReader(stream)
                    Dim result As DataSet = reader.AsDataSet(New ExcelDataSetConfiguration() With {
                                                                 .ConfigureDataTable = Function(__) New ExcelDataTableConfiguration() With {
                                                                 .UseHeaderRow = True}})
                    tables = result.Tables
                    For Each table As DataTable In tables
                End Using
            End Using
        End If
    End Using
End Sub

Use the ExcelReaderFactory to create an instance of the ExcelDataReader, then open the Excel file and read its contents using the ExcelDataReader instance.

Add the SelectedIndexChanged event handler to the Combobox control that allows you to select a sheet name, then load data from DataTable to DataGridView based on the sheet name you selected.

Dim dt As DataTable = tables(cboSheet.SelectedItem.ToString())
DataGridView1.DataSource = dt

In this example, We will open a dialog box that will allow us to select the path to the Excel file. The code will open the Excel file, read its contents, and display them to the DataGridView control. You can modify this code to suit your specific needs, such as processing the data in a different way or integrating it into a larger application.

Using ExcelDataReader libray you can easily connect to the excel file, then read excel file in without oledb.