Add a link to Microsoft.Office.Interop.Excel.
Assuming you have a data repository, and your model looks something like this:
class Contact { public string Firstname { get; set; } public string Lastname { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } }
you can import it into excel like this
Application excelapp = new Application(); excelapp.Visible = true; _Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing)); _Worksheet worksheet = (_Worksheet)workbook.ActiveSheet; worksheet.Cells[1, 1] = "First Name"; worksheet.Cells[1, 2] = "Last Name"; worksheet.Cells[1, 3] = "Email"; worksheet.Cells[1, 4] = "Phone Number"; int row = 1; foreach (var contact in contacts) { row++; worksheet.Cells[row, 1] = contact.Firstname; worksheet.Cells[row, 2] = contact.Lastname; worksheet.Cells[row, 3] = contact.Email; worksheet.Cells[row, 4] = contact.PhoneNumber; } excelapp.UserControl = true;
You can read more about the Excel interaction library here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.11%29.aspx
source share