如何在 datagridview 中导出多个选定的行 vb.net

How to export multiple selected rows in datagridview vb.net

我在导出列表时遇到问题。我想将 DataGridView 中的多个 selected 行导出到 Excel。我在 DataGridView 中添加了一列,它是 chkCheck,另一列是 columncheckbox 我只使用 SQL Server

这是图片。我可能有 10 行数据,但我只想 select 导出其中的几行

这是我的代码:

Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
    Dim xlApp As Excel.Application
    Dim xlWorkBook As Excel.Workbook
    Dim xlWorkSheet As New Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim excelLocation As String = ""


    If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
        excelLocation = FolderBrowserDialog1.SelectedPath & "\Export-" & Format(Now(), "MMMM-dd-yyyy_hh-mm-ss") & ".xlsx"
        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets(1)

        'Export Header Names Start
        Dim columnsCount As Integer = DataGridView1.Columns.Count - 6
        For k As Integer = 1 To DataGridView1.Columns.Count - 6
            xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText
        Next
        'Export Header Name End


        'Export Each Row Start
        Dim i As Integer = 0
        For Each selectedrow In DataGridView1.SelectedRows
            Dim columnIndex As Integer = 0
            Do Until columnIndex = columnsCount
                xlWorkSheet.Cells(i + 2, columnIndex + 1).Value = selectedrow.Cells(columnIndex).Value.ToString
                columnIndex += 1
            Loop
            i += 1
        Next
        'Export Each Row End

        xlWorkSheet.SaveAs(excelLocation)
        xlWorkBook.Close()
        xlApp.Quit()


        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

    Else
        MsgBox("Save have been canceled", MsgBoxStyle.Critical, "Warning")
    End If
End Sub
            'Export Each Row Start
        Dim i As Integer = 0
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim checkselect As Integer = Convert.ToInt16(row.Cells("chkCheck").Value)
            If checkselect = 1 Then
                For columnIndex As Integer = 1 To columnsCount - 1
                    xlWorkSheet.Cells(i + 2, columnIndex + 1).Value = row.Cells(columnIndex).Value.ToString
                Next
                i += 1
            End If
        Next
        'Export Each Row End

这是我尝试导出每一行部分的代码..感谢您的帮助

添加参考 Microsoft.office.Interop.Excel

Private Sub Export_Click(sender As Object, e As EventArgs) 句柄 Button1.Click

    Using sfd As SaveFileDialog = New SaveFileDialog()
        sfd.Filter = "Excel Workbook|*.xls"
        sfd.ValidateNames = True
        If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim rowstotal, colstotal, i, j, k As Integer
            Dim excelapp As New Excel.Application
            Try
                Dim excelbook As Excel.Workbook = excelapp.Workbooks.Add
                Dim excelworksheet As Excel.Worksheet = CType(excelbook.Worksheets(1), Excel.Worksheet)

                rowstotal = DataGridView1.RowCount
                colstotal = DataGridView1.Columns.Count - 1
                With excelworksheet
                    For k = 0 To colstotal
                        .Cells(1, k + 1).value = DataGridView1.Columns(k).HeaderText
                    Next

                    For i = 0 To rowstotal - 1 ' rows details
                        Dim selectcl As Integer = Convert.ToInt16(DataGridView1.Rows(i).Cells("chkcolumn").Value)
                        If selectcl = 1 Then
                            For j = 0 To colstotal
                                .Cells(i + 2, j + 1).value = DataGridView1.Rows(i).Cells(j).Value
                            Next j
                        End If
                    Next i

                    .Rows("1:1").font.fontstyle = "Bold"
                    .Rows("1:1").font.size = 12
                End With
                excelbook.SaveAs(sfd.FileName)
                excelapp.Quit()
                MessageBox.Show("Exported Sucessfully")
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If

    End Using

End Sub