title: Opening text containing CSV with Excel from ASP.NET
body: <p>I'm converting an older asp.net intranet application over to .net 4.5. One of the parts I'm having trouble with is supposed to open a string containing CSV data into Excel.</p>
  
  <p>No matter how I attempt to accomplish this, the resulting excel document contains the contents of the website rather than the text I supplied.</p>
  
  <p>On an additional note, I currently have the aspx in a web project, while the utility/business classes reside in another. I don't know if that is part of the problem.</p>
  
  <p>Please help me out with this issue.</p>
  
  <p><strong>CSV sample:</strong></p>
  
  <pre><code>string text = "\"Year\", \"Make\", \"Model\", \"Length\"\n\"1997\", \"Ford\", \"E350\", \"2.34\"\n\"2000\", \"Mercury\", \"Cougar\", \"2.38\""
  </code></pre>
  
  <p><strong>Code Sample:</strong></p>
  
  <pre><code>private static void OpenCsvFile(string text, string name)
  {
      var httpContext = HttpContext.Current;
      httpContext.Response.Clear();
      httpContext.Response.ContentType = "application/vnd.ms-excel";
      httpContext.Response.Charset = "";
      httpContext.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name + ".csv");
      httpContext.Response.Write(text);
      httpContext.Response.Flush();
      httpContext.ApplicationInstance.CompleteRequest();
  }
  </code></pre>
  
  <p><strong>Update:</strong> The resulting file contains the website contents rather than the CSV text.</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
    &lt;head id="Head1"&gt;
      &lt;title&gt;
        "My WebSite"
      &lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
      &lt;link href="css/style.css" type="text/css" title="WebSite" rel="stylesheet" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form method="post" action="Main.aspx" id="form1" name="form1"&gt;
        &lt;table border="0" cellspacing="0" cellpadding="0" id="container"&gt;
          &lt;td class="logo" onclick="window.location='Main.aspx';"&gt;
            &lt;div class="logoDisplay"&gt;
              &lt;img src="images/logo.gif" alt="" width="280px" height="67px" /&gt;
            &lt;/div&gt;
          &lt;/td&gt;
  ... etc
  </code></pre>