I have the code to parse data to a Datagridview and then export that into an excel spreadsheet. How can I export the headers of the datagridview?
var bs3 = new BindingSource { DataSource = query };
dataGridView1.AutoGenerateColumns = true;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = bs3;
}
}
private void RunExcel()
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}