How to Read Excel File in Visual Basic.Net

5
4892

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:

  • Step 6: Add this following code to Form_Load Event

 

Step 7: To get the value from Excel cell to TextBox. Add this following code to the Button Click Event.

Step 8: Then finally, add this code to save the Excel file and closes it.

 

  • Step 9: Click F5 to run the program.

Inquiries

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:

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.

 

Looking for more source code? Type your keyword here here!

5 COMMENTS

  1. You are absolutely right but finding a solution that does work is near impossible. I have attempted to modify this (and other) code to get the cell value to no avail.

  2. The above did NOT work for me, but it was a good starting point. Ultimately, I reached a solution, here it is:

    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

    Imports Excel = Microsoft.Office.Interop.Excel

    ‘ use the Following code in your application

    Dim FileName As String
    Dim ExcelApp As New Excel.Application
    Dim worksheet As Excel.Worksheet
    Dim workbook As Excel.Workbook
    Dim oRange As Excel.Range

    OpenFileDialog1.InitialDirectory = “C:\”
    OpenFileDialog1.Filter = “Excel File | *.xlsx”

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    ‘ If the Excel File is selected by the user, then do the following….

    FileName = OpenFileDialog1.FileName ‘ Set the File Name to a string variable “FileName”
    workbook = ExcelApp.Workbooks.Open(FileName) ‘ Open the Excel File
    worksheet = workbook.Worksheets(“Sheet1”) ‘ Open “Sheet1”
    oRange = worksheet.Range(“A1”) ‘ Set the name of the cell to read/write

    Dim str1 As String
    str1 = oRange.Value ‘ Read the cell of the selected Sheet1 and the selected Excel File

    oRange = worksheet.Range(“E5”) ‘ Set the name of the cell to read/write
    oRange.Value = “ABC” ‘ Write a value into cell “A5”

    ExcelApp.DisplayAlerts = False ‘ Turn OFF Notifications such as “Do you want to save File?”
    ExcelApp.SaveWorkspace() ‘ however, please save the changes to the Excel File
    ExcelApp.Quit() ‘ Important: Release the resources used by this Application

    End If

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.