title: How do I Response.Write from a Sandboxed Solution?
body: <p>I have a requirement to create an HTML table and shoot it out to the user as a spreadsheet.  I've done this many times in plain old ASP.NET, but it's kicking my behind in SharePoint.  It's a simple Button with an event handler that calls 3 files stored in a Module (accessible to a sandboxed solution) and then tries to shove the text out to the user along with the appropriate content-type and content-disposition.  When I get this part working, the SampleData.txt portion will be replaced with data from multiple lists.  Unfortunately, the page does it's postback, but no file is offered to the user for opening or download.  </p>
  
  <p>I've read in a few places that Response.Write is not usable in web parts, but surely there is some other way of accomplishing this task...?  Here's what I want to do - is there a usable alternative?</p>
  
  <pre><code>    protected override void CreateChildControls()
      {
          Button ReportButton = new Button();
          ReportButton.Text = "Generate DCPDS Report";
          ReportButton.Click += new EventHandler(ReportButton_Click);
          this.Controls.Add(ReportButton);
      }
  
      protected void ReportButton_Click(Object sender, EventArgs e)
      {
          string header = "";
          string sampleData = "";
          string footer = "";
  
          using (SPWeb webRoot = SPContext.Current.Site.RootWeb)
          {
              header = webRoot.GetFileAsString("SpreadsheetParts/Header.txt");
              sampleData = webRoot.GetFileAsString("SpreadsheetParts/SampleData.txt");
              footer = webRoot.GetFileAsString("SpreadsheetParts/Footer.txt");
          }
  
          StringBuilder sb = new StringBuilder();
          sb.Append(header);
          sb.Append(sampleData);
          sb.Append(footer);
  
          Page.Response.Clear();
          HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
          HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=June2010Spreadsheet.xls");
          HttpContext.Current.Response.Write(sb.ToString());
          //HttpContext.Current.Response.End();
      }
  </code></pre>