Visual Basic - 从 Excel 导入选定的行和列

Visual Basic - 从 Excel 导入选定的行和列

有没有办法将 Excel 中的多个数据范围添加到 Visual Basic?我已成功将单元格 A6 到 B260 导入数据网格,但我还想要列 E6-E260 和 G6-G260。(selectedNOD 是选定的电子表格)

    Dim selectedNOD As String
    selectedNOD = ListNOD.SelectedItem
    Try
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim dataSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
        Dim path As String = "NOD.xlsx"

        MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" + selectedNOD + "$A6:B260]", MyConnection)

        dataSet = New System.Data.DataSet
        MyCommand.Fill(dataSet)
        DataGridView1.DataSource = dataSet.Tables(0)

        MyConnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message.ToString)
    End Try

答案1

这段代码解决了我的问题:

Me.dataGridView1.Columns("客户ID").Visible = False

相关内容