I have this export function that allows me to export 2 grid views into 2 separated worksheets in one excel.
But my problems are:
How can I have a usual
you haven't added worksheet in Workbook
try below code
worksheet = (Excel._Worksheet)oXL.Worksheets.Add();
worksheet.Name = SheetName;
for more info check http://dotnetmentors.com/c-sharp/how-to-export-sql-data-to-excel-using-microsoft-office-interop.aspx
set your headers using rangs
string[] colNames = new string[gv.Columns.Count];
int col = 0;
foreach(gvvolumns dc in GridView.Columns)
colNames[col++] = dc.ColumnName;
char lastColumn = (char)(65 + dtProducts.Columns.Count - 1);
oSheet.get_Range("A1", lastColumn + "1").Value2 = colNames;
oSheet.get_Range("A1", lastColumn + "1").Font.Bold = true;
oSheet.get_Range("A1", lastColumn + "1").VerticalAlignment
= Excel.XlVAlign.xlVAlignCenter;