我正在尝试将特定数据从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
我希望标题中的。(点)可能有问题..有没有办法解决它..
答案 0 :(得分:2)
什么是part
,什么是desc
?
如果您的列的标题是part.desc
,那么您应该将其括在方括号中,因为它包含一个特殊字符。
试试这个:
Select [part#desc] from [Allinone$]
编辑:由于某些原因,当与OLEDB绑定时,excel不喜欢标题中的点。在查询中用#替换点。