In
this article we can retrieve Excel Data To data Gridview .It only support Extended Properties Excel version 8.0(Microsoft 2000
version).
Program
Module1.vb code
Imports
System.Data.OleDb
Module
Module1
Public con As
OleDbConnection
Sub pintu(ByVal
s As String)
con =
New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;
" & _
"data
source='" & s & " '; " &
"Extended Properties=Excel 8.0;")
End Sub
Public com As
OleDbCommand
Public ds As
DataSet
Public oledbda As
OleDbDataAdapter
Public dt As
DataTable
Public str As String
End Module
Form1.vb code
Imports
System.Data.OleDb
Public Class Form1
Private Sub
btnbrowse_Click(ByVal sender
As System.Object, ByVal
e As System.EventArgs)
Handles btnbrowse.Click
Dim openfiledialog1 As New OpenFileDialog
openfiledialog1.ShowDialog()
openfiledialog1.Filter = "allfiles|*.xls"
TextBox1.Text =
openfiledialog1.FileName
End Sub
Private Sub
btndisplay_Click(ByVal sender
As System.Object, ByVal
e As System.EventArgs)
Handles btndisplay.Click
pintu(TextBox1.Text)
Try
con.Open()
str =
"select * from [sheet1$]"
com =
New OleDbCommand(str, con)
ds =
New DataSet
oledbda =
New OleDbDataAdapter(com)
oledbda.Fill(ds, "[sheet1$]")
con.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "[sheet1$]"
Catch ex As
Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Output
