locked
How to import data from excel to datagridview by visual basic RRS feed

  • Question

  • Hi,

        i need to import data from excel sheet to datagridview of my vbapplication.Please help me with code.If

    possible with a easy example.

     

    thanks in advance.


    rabbi
    Wednesday, January 11, 2012 4:21 PM

Answers

  • Imports System.Data.SqlClient
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            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='c:\vb.net-informations.xls';Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
            MyCommand.TableMappings.Add("Table", "Net-informations.com")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()
    
        End Sub
    End Class
    

    Already discused in this link.. Check this

    http://social.msdn.microsoft.com/Forums/en-AU/netfx64bit/thread/8b71e730-f93e-4ca7-aef6-8391417c4e13


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
    Wednesday, January 11, 2012 4:29 PM

All replies

  • Imports System.Data.SqlClient
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            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='c:\vb.net-informations.xls';Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
            MyCommand.TableMappings.Add("Table", "Net-informations.com")
            DtSet = New System.Data.DataSet
            MyCommand.Fill(DtSet)
            DataGridView1.DataSource = DtSet.Tables(0)
            MyConnection.Close()
    
        End Sub
    End Class
    

    Already discused in this link.. Check this

    http://social.msdn.microsoft.com/Forums/en-AU/netfx64bit/thread/8b71e730-f93e-4ca7-aef6-8391417c4e13


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
    Wednesday, January 11, 2012 4:29 PM
  • jo swa,

    thanks.i got it.


    rabbi
    Wednesday, January 11, 2012 5:08 PM
  • Please Use Above code. It is working and I Test It.

    So You can not add any excel dll for this code.

    Friday, January 24, 2014 5:05 AM
  • I Have got code to import excel file in datagrideview but I have some further problem in it.

    I actually adding a back up and restore feature to my vb.net project, for that I have added a export to excel from datagriedview for back up and to restore I am using import form excel. this code help me to import excel file to me datagrideview into new dataset. now I want to save the imported file into the main data source. I also tried some changers to the above code to import excel file into my old data source but it only import data of all the row of one column that to of meddle one....

    so please let me know what to do with it.. 

    Saturday, May 3, 2014 12:06 PM