C Sharp And Ms Access Database Tutorial 16 How To Import Excel File To Datagridview

C# And Ms Access Database Tutorial #16 – How To Import Excel File To Datagridview In C# Windows Application

VIDEO TUTORIAL

C# And Ms Access Database Tutorial #16 – How To Import Excel File To Datagridview In C# Windows Application

This Tutorial Shows How To Import Excel File To Datagridview In C# Windows Forms Application using visual studio 2010.

C# SOURCE CODE

       //Import Excel To DataGridView
        private void btnImportExcelToDataGridView_Click(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Excel._Application importExcelToDataGridViewApplication = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Range importExcelToDataGridViewRange;


                try
                {
                    //Open File Dialog Box
                    OpenFileDialog importExcelToDataGridViewOpenFileDialog = new OpenFileDialog();
                    //Dialog Box Title
                    importExcelToDataGridViewOpenFileDialog.Title = "Select Excel File";
                    //Excel File Extensions To be Filtered 
                    importExcelToDataGridViewOpenFileDialog.Filter = "Excel Files | *.xls;*.xlsx;*.xlsm";
                    //Open Dialog box
                    DialogResult importExcelToDataGridViewDialogResult = importExcelToDataGridViewOpenFileDialog.ShowDialog();
                    //if you click open button on opened dialog box
                    if (importExcelToDataGridViewDialogResult == DialogResult.OK)
                    {
                        Microsoft.Office.Interop.Excel._Workbook importExcelToDataGridViewWorkbook = importExcelToDataGridViewApplication.Workbooks.Open(importExcelToDataGridViewOpenFileDialog.FileName);
                        Microsoft.Office.Interop.Excel._Worksheet importExcelToDataGridViewWorksheet = importExcelToDataGridViewWorkbook.ActiveSheet;
                        importExcelToDataGridViewRange = importExcelToDataGridViewWorksheet.UsedRange;

                        for (int excelRows = 2; excelRows <= importExcelToDataGridViewRange.Rows.Count; excelRows++)
                        {
                            Image imageFromExcel = Image.FromFile(importExcelToDataGridViewWorksheet.Cells[excelRows, 8].Value);
                            //Convert Image To Byte Array
                            byte[] imageByteArray = convertImageToByteArray(imageFromExcel);
                            //Populate Datagridview from excel file
                            dataGridView1.Rows.Add(importExcelToDataGridViewRange.Cells[excelRows, 1].Value, importExcelToDataGridViewRange.Cells[excelRows, 2].Value, importExcelToDataGridViewRange.Cells[excelRows, 3].Value, importExcelToDataGridViewRange.Cells[excelRows, 4].Value, importExcelToDataGridViewRange.Cells[excelRows, 5].Value, importExcelToDataGridViewRange.Cells[excelRows, 6].Value, importExcelToDataGridViewRange.Cells[excelRows, 7].Value, importExcelToDataGridViewRange.Cells[excelRows, 8].Value, imageByteArray);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    //Release Resources
                    importExcelToDataGridViewApplication.Quit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Leave a Reply

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