为什么我的代码将第二行作为标题而不是第一行

时间:2019-06-10 22:56:05

标签: excel vb.net

我的代码正在获取第二行记录,将它们作为标头,而我希望它获得第一行记录作为听者(代码正在跳过第一行)

注意:正在遍历各列

我已经编写了代码以遍历excel工作表以获取工作表的标题。它可以正常工作,但不是获取第一行记录,而是跳过并获取第二行记录 注意:正在遍历列

Dim path As String = txtPath.Text
Dim excel_connection As OleDbConnection
Dim dt As DataTable
Dim cmd As OleDbDataAdapter
'Dim sql As String
'Dim result As Boolean
Dim emp_type_id As String = ""
Dim branch_id As String = ""
Dim bank_id As String = ""
excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0 Xml;")
cmd = New OleDbDataAdapter("SELECT * FROM [sheet$]", excel_connection)
dt = New DataTable
cmd.Fill(dt)
For Each column As DataColumn In dt.Columns
    Dim columnName As String = dt.Rows(0)(column.ColumnName).ToString()
    If columnName = "" Then
    Else
        MsgBox(columnName)
    End If
Next

我希望它遍历第一行 注意:正在遍历列

1 个答案:

答案 0 :(得分:0)

尝试将HDR = No添加到OleDbConnection扩展属性中。如下所示:

excel_connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0 Xml;HDR=No;IMEX=1;';")