从Excel导入特定数据到datagrid vb.net
我想从excel中导入特定数据到datagrid,并能够使用下面的查询将所有excel数据导入到datagrid中
从[Allinone $]中select*
下面还查询工作文件
从[Allinone $]中select状态
但下面的查询不工作
从[Allinone $]中selectpart.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时,不喜欢标题中的点。 在查询中用#replace点。