I'm trying to send a view that contains a html table as a downloadable file to the user, as an excel file.
I keep getting the error "Server cannot set content type after HTTP headers have been sent.". I can't figure out what's going wrong...
Here's some code:
Excel.aspx:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<html>
<head runat="server">
    <title>Excel</title>
</head>
<body>
....
</body>
</html>
ControllerAction:
   public FileResult Excel()
    {
        string view = RenderViewToString(this.ControllerContext, "~/Views/Shared/Excel.aspx", null, this.ViewData, this.TempData);
        MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(view));
        string mimetype = RainbowsDotNet.FileHandling.MimeType.GetMimetypeFromExtension(".xls");
        FileStreamResult filestreamresult = new FileStreamResult(stream, mimetype);
        filestreamresult.FileDownloadName = "Employees_{0}.xls".FormatWith(DateTime.Now.ToString("ddMMyyyy_HHmmss"));
        return filestreamresult;
    }
While debugging, string "view" contains:
"\r\n<html>\r\n<body>............................"
Any idea? I do about the exact same thing with a blob and that nicely returns a document to download.
 
     
     
    