将特定数据从excel导入到datagrid vb.net

时间:2013-09-16 07:25:32

标签: sql vb.net excel datagridview

我正在尝试将特定数据从excel导入到datagrid,并能够使用以下查询将所有excel数据导入datagrid

  

从[Allinone $]

中选择*

也低于查询工作文件

  

从[Allinone $]

中选择状态

但低于查询无法正常工作

  

从[Allinone $]

中选择part.desc

我的代码在

之下
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

我希望标题中的。(点)可能有问题..有没有办法解决它..

1 个答案:

答案 0 :(得分:2)

什么是part,什么是desc

如果您的列的标题是part.desc,那么您应该将其括在方括号中,因为它包含一个特殊字符。

试试这个:

Select [part#desc] from [Allinone$]

编辑:由于某些原因,当与OLEDB绑定时,excel不喜欢标题中的点。在查询中用#替换点。