How to Get Data From Excel Show on Form in VB.Net


If you want to to get data from excel show in form you can
do it by use code below.
1. You must have excel file that have data


2. Open Visual Basic and Design form as below

3. Write code below


Imports System.Data

Public Class ComboBox

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

Dim MyConnection As System.Data.OleDb.OleDbConnection

Dim DtSet As System.Data.DataSet

Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

MyConnection = New System.Data.OleDb.OleDbConnection _

("provider=Microsoft.Jet.OLEDB.4.0; “ _

“ Data Source='E:\Computer Magazine\ReadDataFromExcel\ _

ReadDataFromExcel\Employee.xlsx'; “ Extended Properties=Excel 8.0;")

MyConnection.Open()

MyCommand = New System.Data.OleDb.OleDbDataAdapter _

("Select * From [Sheet1$]", MyConnection)

MyCommand.TableMappings.Add("Table", "TestTable")

DtSet = New System.Data.DataSet

MyCommand.Fill(DtSet)

DataGridView1.DataSource = DtSet.Tables(0)

MyConnection.Close()

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

End Class

4. Show result





No comments:

Post a Comment