Sathish Kothandam
Sathish Kothandam

Reputation: 1520

Import specific data from excel to datagrid vb.net

I'm trying to import the specific data from excel to datagrid and able to import the all excel data into datagrid using the below query

Select *from [Allinone$]

also below query also working file

Select status from [Allinone$]

But below query not working

Select part.desc from [Allinone$]

And my code is below

Try
         Dim filename As String
         Dim ofd As New OpenFileDialog
         ofd.Title = "Please select the excel which you want to import"
         If ofd.ShowDialog = DialogResult.OK Then
             filename = ofd.FileName
             Dim strin As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & filename & ";Extended Properties=""Excel 12.0;HDR=YES"";"
             Dim con As OleDbConnection = New OleDbConnection(strin)
             If con.State = ConnectionState.Closed Then
                 con.Open()
                 Dim command As OleDbCommand = New OleDbCommand()
                 command.CommandText = "Select status from [Allinone$]"
                 command.Connection = con
                 Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
                 adapter.SelectCommand = command
                 Dim dt As New DataSet
                 adapter.Fill(dt, "AllTickets")
                 DataGridView1.DataSource = dt.Tables(0)
             End If
         Else
             MsgBox("Havn't selected the file,Form Gonna end now")
             Exit Sub
         End If
 Catch ex As Exception
     MsgBox(ex.ToString)
 End Try

I hope there might be problem with .(dot) in header .. Is there any way to fix it..

Upvotes: 1

Views: 3648

Answers (1)

Pradeep Kumar
Pradeep Kumar

Reputation: 6979

What is part and what is desc?

If the header of your column is part.desc then you should enclose it in square brackets, since it contains a special character.

Try this:

Select [part#desc] from [Allinone$]

EDIT : For some reason excel doesn't like dots in header when binding with OLEDB. Replace dots with # in your query.

Upvotes: 2

Related Questions