title: the file you are trying to open is in a different format than specified by the file extension in Asp.Net
body: <p>the file you are trying to open is in a different format than specified by the file extension c# error when trying to open file in excel.</p>
  <p>Here is my code</p>
  <pre><code>public ActionResult Export(string filterBy)
  {
      MemoryStream output = new MemoryStream();
      StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
  
      var data = City.GetAll().Select(o =&gt; new
      {
          CountryName = o.CountryName,
          StateName = o.StateName,
          o.City.Name,
          Title = o.City.STDCode
      }).ToList();
      var grid = new GridView { DataSource = data };
      grid.DataBind();
      var htw = new HtmlTextWriter(writer);
  
      grid.RenderControl(htw);
  
      writer.Flush();
      output.Position = 0;
  
      return File(output, &quot;application/vnd.ms-excel&quot;, &quot;test.xls&quot;);
  
  }
  </code></pre>
  <p>when am trying to open excel i get this error</p>
  <blockquote>
  <p>the file you are trying to open is in a different format than
  specified by the file extension</p>
  </blockquote>
  <p><img src="https://i.stack.imgur.com/DKmXl.png" alt="enter image description here" /></p>
  <p>After clicking on Yes the file open properly. but i don't want this msg to appear.</p>