Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 16th, 2010  |  syntax: C#  |  size: 2.53 KB  |  views: 227  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. public ActionResult ExportMailReportToExcel()
  3.         {
  4.             DateTime from;
  5.             DateTime to;
  6.             DateTime.TryParse(TempData["fromDTTM"].ToString(),out from);
  7.             DateTime.TryParse(TempData["toDTTM"].ToString(),out to);
  8.             from = from == DateTime.MinValue ? DateTime.Today : from;
  9.             to = to == DateTime.MinValue ? DateTime.Today : to;
  10.             List<pGetMailsSentReportWithoutPageResult> allUsers;
  11.             var users = _profileService.GetMailSenderReportWithoutPage(from, to);
  12.             allUsers = users.ToList<pGetMailsSentReportWithoutPageResult>();
  13.             Response.AddHeader("Content-Disposition", "filename=MailSent-" + TempData["fromDTTM"].ToString()+"-to-"+TempData["toDTTM"].ToString()+ ".xls");
  14.             Response.ContentType = "application/vnd.ms-excel";
  15.             Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
  16.             Response.Write("<head>");
  17.             Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\">");
  18.             Response.Write("<!--[if gte mso 9]>");
  19.             Response.Write("<xml>");
  20.             Response.Write("<x:ExcelWorkbook>");
  21.             Response.Write("<x:ExcelWorksheets>");
  22.             Response.Write("<x:ExcelWorksheet>");
  23.             Response.Write("<x:WorksheetOptions>");
  24.             //these 2 lines draw the gridlines into the Excelsheet
  25.             Response.Write("<x:Panes>");
  26.             Response.Write("</x:Panes>");
  27.             Response.Write("</x:WorksheetOptions>");
  28.             Response.Write("</x:ExcelWorksheet>");
  29.             Response.Write("</x:ExcelWorksheets>");
  30.             Response.Write("</x:ExcelWorkbook>");
  31.             Response.Write("</xml>");
  32.             Response.Write("<![endif]-->");
  33.             Response.Write("</head>");
  34.             Response.Write("<body>");
  35.             Response.Write("<table>");
  36.             Response.Write("<tr>  <th>  Recipient Name  </th> <th> E-Mail </th> <th> Subject </th> <th> Date </th> </tr>");
  37.             foreach (var user in allUsers)
  38.             {
  39.                 Response.Write("<tr>  <td>" + user.PersonName.ToString() + "</td>");
  40.                 Response.Write("<td>" + user.ToAddress.ToString() + " </td>");
  41.                 Response.Write("<td>" + user.Subject.ToString() + "</td>");
  42.                 Response.Write("<td>" + user.CWhen.ToString() + "</td> </tr>");
  43.             }
  44.             Response.Write("</table>");
  45.             Response.End();
  46.             return View("MailSentReport");
  47.         }