Home > Net >  How to export multiple selected rows in datagridview vb.net
How to export multiple selected rows in datagridview vb.net

Time:07-04

I have a problem with exporting a list. I want to export multiple selected rows in a DataGridView to Excel. I have added one column in the DataGridView which is chkCheck and it is columncheckbox for the other I just use SQL Server

enter image description here

Here is the picture. I might have 10 rows of data but I want to select only several of them to export

Here is my code:

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

CodePudding user response:

Add Reference Microsoft.office.Interop.Excel

Private Sub Export_Click(sender As Object, e As EventArgs) Handles 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

CodePudding user response:

            '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

this my code that i try for export each row part ..thanks for helping

  • Related