简体   繁体   English

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

[英]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我正在尝试将特定数据从 excel 导入数据网格,并能够使用以下查询将所有 excel 数据导入数据网格

Select *from [Allinone$]从[Allinone$]中选择*

also below query also working file也在下面查询也是工作文件

Select status from [Allinone$]从 [Allinone$] 中选择状态

But below query not working但下面的查询不起作用

Select part.desc from [Allinone$]从 [Allinone$] 中选择 part.desc

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..我希望标题中的 .(dot) 可能有问题..有什么办法可以解决它..

What is part and what is desc ?什么是part ,什么是desc

If the header of your column is part.desc then you should enclose it in square brackets, since it contains a special character.如果您的列的标题是part.desc那么您应该将它括在方括号中,因为它包含一个特殊字符。

Try this:试试这个:

Select [part#desc] from [Allinone$]

EDIT : For some reason excel doesn't like dots in header when binding with OLEDB.编辑:出于某种原因,当与 OLEDB 绑定时,excel 不喜欢标题中的点。 Replace dots with # in your query.在查询中用 # 替换点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM