(Visual Studio 2010) How To Export DataGridView To Excel In Visual Basic Net Windows Application – Microsoft Excel 2010

(Visual Studio 2010) How To Export DataGridView To Excel In Visual Basic .Net Windows Application – Microsoft Excel 2010

How To Export DataGridView To Excel In Visual Basic .Net Windows Application
How To Export DataGridView To Excel In Visual Basic Net DGV
How To Export DataGridView To Excel In Visual Basic Net DGV

This Tutorial Shows How To Export DataGridView Data To Excel In Visual Basic .Net Windows Application.

How To Export DataGridView To Excel In Visual Basic Net
How To Export DataGridView To Excel In Visual Basic Net

Visual Basic .Net  SOURCE CODE

        Dim exportDatagridviewToExcelVBNetApp As New Application
        Dim exportDatagridviewToExcelVBNetWorkBook As Workbook
        Dim exportDatagridviewToExcelVBNetWorkSheet As Worksheet
        Dim columnsDGV As Integer
        Dim rowsToExportDGV As Integer
        Dim columnsToExportDGV As Integer

        Try
            exportDatagridviewToExcelVBNetWorkBook = exportDatagridviewToExcelVBNetApp.Workbooks.Add(Type.Missing)
            exportDatagridviewToExcelVBNetWorkSheet = exportDatagridviewToExcelVBNetWorkBook.Sheets("Sheet1")
            exportDatagridviewToExcelVBNetWorkSheet = exportDatagridviewToExcelVBNetWorkBook.ActiveSheet
            'Excel Sheet Name
            exportDatagridviewToExcelVBNetWorkSheet.Name = "ExportedDataFromDataGridView"

            'Getting Header Row
            For columnsDGV = 1 To DataGridView1.Columns.Count - 1
                exportDatagridviewToExcelVBNetWorkSheet.Cells(1, columnsDGV) = DataGridView1.Columns(columnsDGV - 1).HeaderText()
            Next

            'Getting Normal Rows
            For rowsToExportDGV = 0 To DataGridView1.RowCount - 1

                For columnsToExportDGV = 0 To DataGridView1.ColumnCount - 2
                    exportDatagridviewToExcelVBNetWorkSheet.Cells(rowsToExportDGV + 2, columnsToExportDGV + 1) = DataGridView1(columnsToExportDGV, rowsToExportDGV).Value.ToString()

                Next

            Next



            Dim exportDatagridviewToExcelSaveFileDialog As New SaveFileDialog()
            'Initial directory when you click export button
            exportDatagridviewToExcelSaveFileDialog.InitialDirectory = "C:\Users\Authentic\Desktop"
            'File name Displayed on the DialogBox
            exportDatagridviewToExcelSaveFileDialog.FileName = "ExportDatagridViewToExcel"
            'Excel file Name Extension
            exportDatagridviewToExcelSaveFileDialog.DefaultExt = "xlsx"

            If exportDatagridviewToExcelSaveFileDialog.ShowDialog() = DialogResult.OK Then
                exportDatagridviewToExcelVBNetWorkBook.SaveAs(exportDatagridviewToExcelSaveFileDialog.FileName)
                MessageBox.Show("Excel Exported Successfully")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.StackTrace)
        Finally

        End Try


How To Export DataGridView To Excel In Visual Basic Dot Net
How To Export DataGridView To Excel In Visual Basic Dot Net

Leave a Reply

Your email address will not be published. Required fields are marked *