I have a program which needs to query from a database and export the resulting datatable to an excel sheet. I already have code that is correctly doing these things. However, the resulting excel sheet doesn't have the column names that the datatable is. Thus, the excel sheet just has many columns of numbers without any lables, so it's fairly useless since the reader doesn't know what the information stands for.
My question is how would I set the column names in my excel sheet to be the the ones in my datatable. Here is the general relevant code.
Code:// Create Excel File object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); Microsoft.Office.Interop.Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // This is the point I wish to add the column names. I was thinking that a for loop // going through all the columns in Report and copying them to xlWorkSheet // would work, but I can't find anything (such as a property) of xlWorkSheet that // would allow me to do this. // Transfer Data from Report to excel file // Report is a DataGridView for (i = 0; i <= Report.DataGridView.RowCount - 1; i++) { for (j = 0; j <= Report.DataGridView.ColumnCount - 1; j++) { DataGridViewCell cell = Report.DataGridView[j, i]; xlWorkSheet.Cells[i + 1, j + 1] = cell.Value; } }