How to Read an Excel file in Visual Basic.Net
In this tutorial, I will teach you how to read an excel file in Visual basic.net (VB.Net).
This tutorial can be integrated to your personalized vb.net project.
So let’s get started:
Step How to Read an Excel file in Visual Basic.Net
- Step 1: First is open the Visual Basic, Select File on the menu, then click New and create a new project.
- Step 2: Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK
- Step 3: Design your form like this just like what I’ve shown you below.
Add a 2 Textbox and a Button
- Step 4: After designing your form. Add a reference to Microsoft Excel 12.0 Object Library
In your project menu click on Project– Add Reference – go to COM tab
Add Microsoft Excel 12.0 Object Library
- Step 5: Then, go to the code page and add the following declarations:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim APP As New Excel.Application
Dim worksheet As Excel.Worksheet
Dim workbook As Excel.Workbook
- Step 6: Add this following code to Form_Load Event
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
workbook = APP.Workbooks.Open("C:\Users\Clive\Documents\Visual Studio 2008\clive projects\writeandread\writeandread\bin\Debug\sales.xlsx")
worksheet = workbook.Worksheets("sheet1")
Step 7: To get the value from Excel cell to TextBox. Add this following code to the Button Click Event.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
TextBox1.Text = worksheet.Cells(1, 1).Value
TextBox2.Text = worksheet.Cells(1, 2).Value
Step 8: Then finally, add this code to save the Excel file and closes it.
Private Sub Form1_FormClosed(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosedEventArgs) _
- Step 9: Click F5 to run the program.
If you have any questions or suggestions about How to Read an Excel file using Visual Basic.Net please contact me through our contact page or simply leave a comment below.
Download the Code Below
Download How to Read an Excel file using Visual Basic.Net Source code Here
Readers might read also:
- How to Export DataGridView Data to Excel using VB.Net
- How to Create an Excel File Using Visual Basic.Net
- Basic Calculator Using Visual Basic.Net
If you want to get started with a little bit of advance topic in VB.Net you may start your lesson here on how to connect access database to vb.net. Watch the video here below.