title: How to create excel file in asp.net?
body: <p>I am very new to asp.net development  </p>
  
  <p>I have presented the user to download the  <code>asp repeater grid details</code> in excel format on button click. I have done it with following code which  I got after googling</p>
  
  <pre><code>            Response.Clear();
              Response.Buffer = true;
              Response.AddHeader("content-disposition", "attachment;filename=ExcelExport.xls");
  
              for (int i = 0; i &lt; RptMasterData.Items.Count; i++)
              {
                  Table tbl = (Table)RptMasterData.Items[i].FindControl("tblTest");
                  tbl.Style.Add("background-color", "#FF9900");
                  GridView gvAllocation = (GridView)RptMasterData.Items[i].FindControl("gvAllocation");
                  gvAllocation.HeaderRow.Style.Add("background-color", "#95c556");
  
                  int j = 1;
                  foreach (GridViewRow gvrow in gvAllocation.Rows)
                  {
                      gvAllocation.BackColor = System.Drawing.Color.White;
                      if (j &lt;= gvAllocation.Rows.Count)
                      {
                          if (j % 2 != 0)
                          {
                              for (int k = 0; k &lt; gvrow.Cells.Count; k++)
                              {
                                  gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                              }
                          }
                      }
                      j++;
                  }
              }
  
              Response.ContentType = "application/vnd.ms-excel";
              Response.ContentEncoding = System.Text.Encoding.Unicode;
              Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
  
              StringWriter sw = new StringWriter();
              HtmlTextWriter hw = new HtmlTextWriter(sw);
  
              //style to format numbers to string
              string style = @"&lt;style&gt; .textmode { mso-number-format:\@; } &lt;/style&gt;";
              Response.Write(style);
              RptMasterData.RenderControl(hw);
              Response.Write("&lt;table&gt;");
              Response.Output.Write(sw.ToString());
              Response.Write("&lt;table&gt;");
              Response.Flush();
              HttpContext.Current.ApplicationInstance.CompleteRequest();
              Response.Close();
  </code></pre>
  
  <p>The file is downloaded as excel file.But when I try to open the file in MS office I get the Error message as   </p>
  
  <p><img src="https://i.stack.imgur.com/Gmqpi.png" alt="enter image description here"></p>
  
  <p>The problem occurred when I try to edit and save the file in Ms-office. then it shows following warning<br>
  <img src="https://i.stack.imgur.com/VlOb8.png" alt="enter image description here"></p>
  
  <p>So I Just tried to open the same in text editor and found that the file downloaded is an HTML file containing the needed data as expected not excel file.</p>
  
  <p>Can anyone guide me in how to create an actual Excel file for downloading when user click "Download"</p>