C Sharp And Ms Access Database Tutorial 17 How To Export Datagridview To Excel File

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

VIDEO TUTORIAL

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

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

C# SOURCE CODE

       //Exporting DataGridView To Excel File On Button Click
        private void btnExportDataGridViewToExcel_Click(object sender, EventArgs e)
        {

            try
            {
                Microsoft.Office.Interop.Excel._Application exportDataGridViewToExcelApplication = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel._Workbook exportDataGridViewToExcelWorkbook = exportDataGridViewToExcelApplication.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet exportDataGridViewToExcelWorksheet = null;
                exportDataGridViewToExcelApplication.Visible = true;
                exportDataGridViewToExcelWorksheet = exportDataGridViewToExcelWorkbook.Sheets["Sheet1"];
                exportDataGridViewToExcelWorksheet = exportDataGridViewToExcelWorkbook.ActiveSheet;
                exportDataGridViewToExcelWorksheet.Name = "ExportDataGridViewToExcel";

                try
                {
                    //Exporting First Row
                    //Export all columns except image column
                    for (int i = 0; i < dataGridView1.Columns.Count - 1; i++)
                    {
                        exportDataGridViewToExcelWorksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                    }

                    //Exporting Other Rows

                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        for (int j = 0; j < dataGridView1.Columns.Count - 1; j++)
                        {
                            //Check If a row is empty first
                            if (dataGridView1.Rows[i].Cells[j].Value != null)
                            {
                                //Export From Second Row
                                exportDataGridViewToExcelWorksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                            }
                            else
                            {
                                //Output Empty String If A Row Is Empty
                                exportDataGridViewToExcelWorksheet.Cells[i + 2, j + 1] = "";
                            }

                        }
                    }

                    //Save Excel File Dialog Box
                    SaveFileDialog exportDataGridViewToExcelSaveFileDialog = new SaveFileDialog();
                    //Excel FileName
                    exportDataGridViewToExcelSaveFileDialog.FileName = "ExportDataGridViewToExcel";
                    //Default Excel File Extension 
                    exportDataGridViewToExcelSaveFileDialog.DefaultExt = ".xlsx";
                    //Dialog Name
                    exportDataGridViewToExcelSaveFileDialog.Title = "Export DataGridView To Excel";
                    //Supported File Extension
                    exportDataGridViewToExcelSaveFileDialog.Filter = "Excel Files (*.xls;*.xlsm;*.xlsx) | *.xls;*.xlsm;*.xlsx";

                    DialogResult exportDataGridViewToExcelDialogResult = exportDataGridViewToExcelSaveFileDialog.ShowDialog();
                    // If Save Button Is Clicked
                    if (exportDataGridViewToExcelDialogResult == DialogResult.OK)
                    {
                        exportDataGridViewToExcelWorkbook.SaveAs(exportDataGridViewToExcelSaveFileDialog.FileName);
                        MessageBox.Show("DataGridView Exported To Excel Succsessfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    //Release Resources
                    exportDataGridViewToExcelWorksheet = null;
                    exportDataGridViewToExcelWorkbook.Close();
                    exportDataGridViewToExcelApplication.Quit();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

Leave a Reply

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