How to Open and Read Excel Files in VB.NET

By FoxLearn 12/10/2024 2:35:26 AM   27.06K
To open and read Excel files using ExcelDataReader, ExcelDataReader.DataSet in VB.NET Windows Forms Application, you need to follow these steps

How to read excel file in vb.net windows application?

Open your Visual Studio, then create a new Windows Forms Application project.

Next, you can design a simple UI that allows you to select an excel file, then open, read and import data from excel file to DataGridView in vb.net as shown below.

vb.net 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.

' vb.net connect to excel file
' vb.net open excel file using exceldatareader vb.net
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
            ' excel net vb file
            Using stream = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read)
                ' vb net read excel file
                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
                    cboSheet.Items.Clear()
                    For Each table As DataTable In tables
                        cboSheet.Items.Add(table.TableName)
                    Next
                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 vb.net without oledb.

If you encounter an error when reading excel file in vb.net as below

No data is available for encoding 1252

You can modify your vb.net code as shown below.

Private Sub btnBrowser_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
        Using ofd As OpenFileDialog = New OpenFileDialog() With {.Filter = "Excel Workbook|*.xlsx|Excel 97-2003 Workbook|*.xls"}
            If ofd.ShowDialog() = DialogResult.OK Then
                txtFileName.Text = ofd.FileName
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
                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
                        cboSheet.Items.Clear()
                        For Each table As DataTable In tables
                            cboSheet.Items.Add(table.TableName)
                        Next
                    End Using
                End Using
            End If
 
        End Using
 
    End Sub

Just add this line before File.Open

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)

VIDEO TUTORIAL