.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Populate one column from excelsheet

Posted By:satyapriyanayak       Posted Date: November 15, 2013    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

We will know how to populate one column from excelsheet.
 

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


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

        Try

            con.Open()

            str = "select * from [sheet1$]"

            com = New OleDbCommand(str, con)

            oledbda = New OleDbDataAdapter(com)

            ds = New DataSet

            oledbda.Fill(ds, "[sheet1$]")

            con.Close()

            dt = ds.Tables("[sheet1$]")

            Dim i As Integer

            For i = 0 To dt.Rows.Count - 1

                ComboBox1.Items.Add(dt.Rows(i).ItemArray(1))

            Next

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

    End Sub

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ComboBox1.Text = "Please select"

    End Sub

End Class



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend