title: Creating and downloading an Excel file from ASPX page
body: <p>I need to create and excel file and download it from an aspx page. I created a simple page (default.aspx) with one button on it ("Button1"). Here is the default.aspx.cs file</p>
  
  <pre><code>using System;
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
      protected void Button1_Click(object sender, EventArgs e)
      {
          string content = "";
          content += "&lt;head&gt;";
          content += "&lt;/head&gt;";
          content += "&lt;body&gt;";
          content += "&lt;table&gt;";
          content += "&lt;tr&gt;";
          content += "&lt;th&gt;foo&lt;/th&gt;";
          content += "&lt;/tr&gt;";
          content += "&lt;/table&gt;";
          content += "&lt;/body&gt;";
          content += "&lt;/html&gt;";
          Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
          Response.AppendHeader("Content-disposition", "attachment; filename=test.xls");
          Response.Write(content);
      }
  }
  </code></pre>
  
  <p>When I run this code and click on the button it does in fact produce and Excel file with the test "foo" in cell A1. The problem is that I get the following error message from IE:</p>
  
  <p><img src="https://i.stack.imgur.com/41XFy.png" alt="error message displayed by Excel"></p>
  
  <p>What am I doing wrong? How I produce the file so that it does not cause an error message to be displayed?</p>