title: Export datagridview to excel sheet rows and headers in C# windows form
body: <p>I need help on exporting the data from a datagridview columns and headers, below I will add the code I am using for the exporting to excel from the datagridview and it works send all the data from the datagridview to the excel sheet the only problem is that the headers are not being send to the sheet. can you guys help with that piece I need to add here so the header also show in the sheet.</p>
  
  <pre><code>private void ImportDataGridViewDataToExcelSheet()
  {
  
              string userID = WindowsIdentity.GetCurrent().Name;
              var userName = userID.Remove(0, 15);
              //test.Text = userName;
              //dataGridView_ShowAllData
              Excel.Application xlApp;
              Excel.Workbook xlWorkBook;
              Excel.Worksheet xlWorkSheet;
              object misValue = System.Reflection.Missing.Value;
  
              Int16 i, j;
  
              xlApp = new Excel.Application();
              xlWorkBook = xlApp.Workbooks.Add(misValue);
  
              xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  
              for (i = 0; i &lt;= dataGridView_ShowAllData.RowCount - 2; i++)
              {
                  for (j = 0; j &lt;= dataGridView_ShowAllData.ColumnCount - 1; j++)
                  {
                      xlWorkSheet.Cells[i + 1, j + 1] = dataGridView_ShowAllData[j, i].Value.ToString();
                  }
              }
  
              xlWorkBook.SaveAs(@"C:\Users\" + userName + "\\Downloads\\ReportView.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
              xlWorkBook.Close(true, misValue, misValue);
              xlApp.Quit();
  
              releaseObject(xlWorkSheet);
              releaseObject(xlWorkBook);
              releaseObject(xlApp);
          }
  
          private void releaseObject(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();
              }
          }
  
          private void toolStripMenuItem_ImportDGVToExcel_Click(object sender, EventArgs e)
          {
              ImportDataGridViewDataToExcelSheet();
          }
  </code></pre>