我正在尝试将特定数据从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
我希望标题中的。(点)可能有问题。有什么办法可以解决它。
什么是part
什么desc
?
如果列的标题为part.desc
,则应将其括在方括号中,因为它包含特殊字符。
试试这个:
Select [part#desc] from [Allinone$]
编辑:由于某些原因,excel与OLEDB绑定时不喜欢标头中的点。在查询中将点替换为#。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句