If you want to create and fill an Excel sheet with c#, you can do this by using Microsoft.Office.Interop.Excel. The smart thing to do is to fill an array with all you values and copy those values in a new excel sheet at once. This i much faster then filling the excel sheet line by line.
// create the excel objects
Application application = new Application();
Workbook workbook = (Workbook)(application.Workbooks.Add(Missing.Value));
Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
// init the values for the array; rows is a variable for the number of rows and columns ... duh
string[,] values = new string[rows, columns];
int rowCount = 0; // use the rowcount to select the array position
// add the first line in the array
values[rowCount, 0] = "Firstname";
values[rowCount, 1] = "Lastname";
values[rowCount, 2] = "Country";
// loop through the data assuming you have a datarow with values and fill the array; all values in the datarow are strings
foreach (DataRow dr in dataRows)
{
rowCount++;
values[rowCount, 0] = dr["Firstname"].ToString();
values[rowCount, 1] = dr["Lastname"].ToString();
values[rowCount, 2] = dr["Country"].ToString();
}
// fill the values of the array in the excel worksheet; with get_Range you provide the range in the excel workbook
worksheet.get_Range("A1", "C" + values.Length.ToString()).Value2 = values;
// you can also format the first line in the excel
worksheet.get_Range("A1", "C1").Font.Bold = true;
application.Visible = true;
application.UserControl = true;
// save and close the workbook; excelFileLocation is the full path to the excel file
workbook.SaveAs(excelFileLocation, XlFileFormat.xlXMLSpreadsheet,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
workbook.Close(false, Missing.Value, false);
// close the application
application.Quit();