How to export data from DataGridView to Excel including header as column C# Visual Studio

//you can change dgv_data_baru to your own DataGridView name
//put these using above the class but first you have to import the referrence, there are many easy guides about it on google
using Microsoft.Office.Interop.Excel;
using _Excel1 = Microsoft.Office.Interop.Excel;
using System.IO;

private void ImportDataGridViewDataToExcelSheet()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Documents (*.xls)|*.xls";
            sfd.FileName = "Inventory_Adjustment_Export.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                _Excel1.Application xlexcel = new _Excel1.Application();
                _Excel1.Application xlApp;
                _Excel1.Workbook xlWorkBook;
                _Excel1.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new _Excel1.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (_Excel1.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                int i = 0;
                int j = 0;

                /*header text*/
                for (i = 0; i <= dgv_data_baru.Columns.Count - 1; i++)
                {
                    xlWorkSheet.Cells[1, i + 1] = dgv_data_baru.Columns[i].HeaderText;
                }

                /*And the information of your data*/
                for (i = 0; i <= dgv_data_baru.RowCount - 1; i++)
                {
                    for (j = 0; j <= dgv_data_baru.ColumnCount - 1; j++)
                    {
                        DataGridViewCell cell = dgv_data_baru[j, i];
                        xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
                    }
                }

                xlWorkBook.SaveAs(sfd.FileName, _Excel1.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, _Excel1.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlexcel.DisplayAlerts = true;
                xlWorkBook.Close(true, misValue, misValue);
                xlexcel.Quit();

                releaseObject1(xlWorkSheet);
                releaseObject1(xlWorkBook);
                releaseObject1(xlApp);
            }
            if (File.Exists(sfd.FileName))
                System.Diagnostics.Process.Start(sfd.FileName);

        }

        private void releaseObject1(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }


Comments

Popular posts from this blog

How to add excel with header to DataGridView C# Visual Studio