Read Excel File in VB.Net

In this tutorial, I will teach you how to read the excel file in vb.net. This kind of function can easily retrieve the data in the excel file and display it in the datagridview. This is just a simple method but can be very helpful in the future use. Let’s begin.

Creating application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic. ps1

Step 2

Do the form just like this. ps2

Step 3

Double click the button to fire the click event handle of it and do the following codes for reading the excel file.
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.  
  3. Dim con As OleDb.OleDbConnection
  4. Dim cmd As New OleDb.OleDbCommand
  5. Dim da As New OleDb.OleDbDataAdapter
  6. Dim dt As New DataTable
  7.  
  8. Try
  9. With OpenFileDialog1
  10. .Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
  11. .FilterIndex = 1
  12. .Title = "Import data from Excel file"
  13. End With
  14. If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
  15. TextBox1.Text = OpenFileDialog1.FileName
  16. con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & TextBox1.Text & " ; " & "Extended Properties=Excel 8.0;")
  17.  
  18. con.Open()
  19.  
  20. With cmd
  21. .Connection = con
  22. .CommandText = "select * from [Sheet1$]"
  23. End With
  24. da.SelectCommand = cmd
  25. da.Fill(dt)
  26. DataGridView1.DataSource = dt
  27. End If
  28.  
  29.  
  30. Catch ex As Exception
  31. MessageBox.Show(ex.Message)
  32. Finally
  33. con.Close()
  34. End Try
  35. End Sub
Press F5 to run your project. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment