(Visual Studio 2010) How To Import Data From Excel To Datagridview In Visual Basic Net Windows Application – Microsoft Excel 2010

How To Import Data From Excel To DataGridView In Visual Basic .Net Windows Application

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

Import Excel file to DataGridView in VB
Import Excel file to DataGridView in VB

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

how to import data from excel to gridview in vb
how to import data from excel to gridview in vb

Visual Basic .Net SOURCE CODE


        Dim importExcelToDatagridviewApp As Application
        Dim importExcelToDatagridviewWorkbook As Workbook
        Dim importExcelToDatagridviewWorkSheet As Worksheet
        Dim importExcelToDatagridviewRange As Range


        Try


            Dim importExcelToDatagridviewOpenFileDialog As New OpenFileDialog()
            'Initial Open Dialog Directory
            importExcelToDatagridviewOpenFileDialog.InitialDirectory = "C:\Users\Authentic\Desktop"
            'Show Only Excel Files When You Click Import Button
            importExcelToDatagridviewOpenFileDialog.Filter = "Excel Files | *.xls;*.xlsx;*.xlsm"
            'Dialog Box Title
            importExcelToDatagridviewOpenFileDialog.Title = "Select Excel File"
            Dim Exelrows As Integer

            If importExcelToDatagridviewOpenFileDialog.ShowDialog() = DialogResult.OK Then


                importExcelToDatagridviewApp = New Application()
                importExcelToDatagridviewWorkbook = importExcelToDatagridviewApp.Workbooks.Open(importExcelToDatagridviewOpenFileDialog.FileName)
                importExcelToDatagridviewWorkSheet = importExcelToDatagridviewWorkbook.Sheets.Item(1)
                importExcelToDatagridviewRange = importExcelToDatagridviewWorkSheet.UsedRange

                'Start From Second Row
                For Exelrows = 2 To importExcelToDatagridviewRange.Rows.Count
                    'Getting Image From Image Path Saved In Excel Cell And Converting It To Byte Array Before Displaying It On DatagaGridView Cell.
                    Dim imagePathFromExcelFileToDataGridView As Image = Image.FromFile(importExcelToDatagridviewRange.Cells(Exelrows, 5).Value.ToString())
                    'Populationg DataGridView Inside a For Loop
                    DataGridView1.Rows.Add(importExcelToDatagridviewRange.Cells(Exelrows, 1).Value.ToString, importExcelToDatagridviewRange.Cells(Exelrows, 2).Value.ToString, importExcelToDatagridviewRange.Cells(Exelrows, 3).Value.ToString, importExcelToDatagridviewRange.Cells(Exelrows, 4).Value.ToString, importExcelToDatagridviewRange.Cells(Exelrows, 5).Value.ToString, convertImageToByteArray(imagePathFromExcelFileToDataGridView))

                Next



            End If

            'importExcelToDatagridviewWorkbook.Close()

        Catch ex As Exception

        Finally

        End Try

How To Import Data From Excel To DataGridView In Vb.Net Windows Application
How To Import Data From Excel To DataGridView In Vb.Net Windows Application

Leave a Reply

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