ARTICLE

Retrieving Excel Data To dataGridview in VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 12, 2011
In this article we can retrieve Excel Data To data Gridview.
Reader Level:

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

table1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor