将特定列从 Excel 导出到 Datagridview

Export Specific Columns from Excel to Datagridview

我有一个包含 120 列和大约 800 行数据的电子表格。我在网上找到了下面的代码,使用 ClosedXml 效果很好。

我不想导出所有行,而是只想获取列 1 和 2(及其 headers)以及列 15 到 46 及其相应的 headers.

有谁知道 ClosedXml 是否作为 call/procedure 来提取特定的列,或者我是否需要将所需的列放入数组中,然后从那里提取所需的内容?我穿着 google 出去看:) .

谢谢

Protected Sub ImportExcel(sender As Object, e As EventArgs)
    'Open the Excel file using ClosedXML.
    Using workBook As New XLWorkbook(FileUpload1.PostedFile.InputStream)
        'Read the first Sheet from Excel file.
        Dim workSheet As IXLWorksheet = workBook.Worksheet(1)
 
        'Create a new DataTable.
        Dim dt As New DataTable()
 
        'Loop through the Worksheet rows.
        Dim firstRow As Boolean = True
        For Each row As IXLRow In workSheet.Rows()
            'Use the first row to add columns to DataTable.
            If firstRow Then
                For Each cell As IXLCell In row.Cells()
                    dt.Columns.Add(cell.Value.ToString())
                Next
                firstRow = False
            Else
                'Add rows to DataTable.
                dt.Rows.Add()
                Dim i As Integer = 0
                For Each cell As IXLCell In row.Cells()
                    dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString()
                    i += 1
                Next
            End If
 
            GridView1.DataSource = dt
            GridView1.DataBind()
        Next
    End Using
End Sub

GetColumnNames 中,我通过从 Excel sheet 中选择一个范围来获取名称。它将是 A1:P1 但将 P 替换为最后一列的内容。

Private strFileName As String = "C:\Users\..." 'Your path with file name
Private ConStr As String = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={strFileName};Extended Properties=""Excel 12.0;HDR=YES;"""

Private Function GetColumnNames() As DataTable
    Dim dt As New DataTable
    Dim sheetname = "Vanguard"
    Using cn As New OleDbConnection(ConStr),
        cmd As New OleDbCommand($"SELECT * FROM [{sheetname}$A1:P1]", cn)
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

GetSelectQuery 中,我使用带有列名的 DataTable 并仅将我们感兴趣的列添加到列表中。使用 String.Join 获取插入到 Select 语句中的单个逗号分隔字符串。

Private Function GetSelectQuery() As String
    Dim sheetname = "Vanguard"
    Dim dt = GetColumnNames()
    Dim fields As New List(Of String)
    'Add the first 2 field names to the list
    For i = 0 To 1
        fields.Add(dt.Columns(i).ColumnName)
    Next
    'Add fields 15 - 46
    For i = 7 To 10
        fields.Add(dt.Columns(i).ColumnName)
    Next
    Dim StringOfFieldNames = String.Join(",", fields)
    Dim SelectQuery = $"Select {StringOfFieldNames} From [{sheetname}$];"
    Debug.Print(SelectQuery) 'Just to check if the Select statement looks correct
    Return SelectQuery
End Function

在 Button.Click 中使用 Select 查询填充 DataTable 并将其作为 DataSource 分配给网格。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim SelectQuery = GetSelectQuery()
    Dim dt As New DataTable
    Using cn As New OleDbConnection(ConStr),
            cmd As New OleDbCommand(SelectQuery, cn)
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
End Sub

谢谢玛丽 :)

你给了我需要的推动力。我所做的只是搜索已经填充了所有列 (119) 和行 (444) 的现有数据集。我建立了另一个数据表并填充了 使用我想要的 col 和行并将其绑定到我的 datagridview。我试图不使用 Oledb,因为我的客户有各种各样的机器,并且 运行 遇到旧的 microsoft.oledb.16.0 提供商未注册问题。这是下面添加的代码。 接下来,我会将所有参数列表包装到某种数组中,以稍微压缩代码。

再次感谢您的帮助:)

    Dim dtmeanavg As New DataTable("meanavg")

        For i = 0 To 1
            dtmeanavg.Columns.Add(dt.Columns(i).ColumnName)
        Next

        For i = 15 To 40
            dtmeanavg.Columns.Add(dt.Columns(i).ColumnName)
        Next

        For i = 1 To dt.Rows.Count - 1

            dtmeanavg.Rows.Add(dt.Rows(i).Item(0),
                            dt.Rows(i).Item(1),
                            dt.Rows(i).Item(2),
                            dt.Rows(i).Item(15),
                            dt.Rows(i).Item(16),
                            dt.Rows(i).Item(17),
                            dt.Rows(i).Item(18),
                            dt.Rows(i).Item(19),
                            dt.Rows(i).Item(20),
                            dt.Rows(i).Item(21),
                            dt.Rows(i).Item(22),
                            dt.Rows(i).Item(23),
                            dt.Rows(i).Item(24),
                            dt.Rows(i).Item(25),
                            dt.Rows(i).Item(26),
                            dt.Rows(i).Item(27),
                            dt.Rows(i).Item(28),
                            dt.Rows(i).Item(29),
                            dt.Rows(i).Item(30),
                            dt.Rows(i).Item(31),
                            dt.Rows(i).Item(32),
                            dt.Rows(i).Item(33),
                            dt.Rows(i).Item(34),
                            dt.Rows(i).Item(35),
                            dt.Rows(i).Item(36),
                            dt.Rows(i).Item(37),
                            dt.Rows(i).Item(38),
                            dt.Rows(i).Item(39)
                                        )
        Next




        dgvInventory.DataSource = dtmeanavg
        Cursor = Cursors.Default
        workBook.Dispose()