title: Export DataTable to Excel?
body: <p>I need code that shows how to export a DataTable out to an Excel spreadsheet.</p>
  
  <pre><code> string data = null;
              int i = 0;
              int j = 0;
              string sFileName=null;
  
              Excel.Application xlApp;
              Excel.Workbook xlWorkBook;
              Excel.Worksheet xlWorkSheet;
              object misValue = System.Reflection.Missing.Value;
  
              xlApp = new Excel.ApplicationClass();
              xlWorkBook = xlApp.Workbooks.Add(misValue);
              xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
  
              for (i = 0; i &lt;= dt.Rows.Count - 1; i++)
              {
                  for (j = 0; j &lt;= dt.Columns.Count - 1; j++)
                  {
                      data = dt.Rows[i].ItemArray[j].ToString();
                      xlWorkSheet.Cells[i + 1, j + 1] = data;
                  }
              }
  
              SaveFileDialog oDialog = new SaveFileDialog();
              oDialog.Filter = "Excel files | *.xls";
              if (oDialog.ShowDialog() == DialogResult.OK)
              {
                  sFileName = oDialog.FileName;
              }
  
              if (sFileName != null)
              {
                  xlWorkBook.SaveAs(sFileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                  xlWorkBook.Close(true, misValue, misValue);
                  xlApp.Quit();
                  MessageBox.Show("Report saved with file: " + sFileName, "To Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
  
  
              }
              else
              {
                  //oEXLApp.ActiveWorkbook.Close(false, Missing.Value, Missing.Value);
                  MessageBox.Show("Cannot export to excel...", "Can't export", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
  
              releaseObject(xlWorkSheet);
              releaseObject(xlWorkBook);
              releaseObject(xlApp);
  </code></pre>
  
  <p>This code can export datatable to excel, but problem is that I don't get the column name.
  I want dt(DataTable)column name show than the value comes.</p>