title: exporting gridview to Ms Excel 2007
body: <p>I am trying to export grid view data into excel 2007 i.e. xlsx format. but its giving error.</p>
  
  <p>i am using following code</p>
  
  <p>protected void Button1_Click(object sender, EventArgs e)</p>
  
  <pre><code>    {
          Response.Clear();
          Response.Buffer = true;
          Response.AddHeader("content-disposition", "attachment;filename=text.xls");
          Response.Charset = "";
          Response.ContentType = "application/vnd.ms-excel";
          StringWriter sw = new StringWriter();
          HtmlTextWriter hw = new HtmlTextWriter(sw);
          GridView1.DataSource = Presenter.CurrentModel.BillStatementGlModelRecords;
          GridView1.DataBind();
          GridView1.AllowPaging = false;
  
          GridView1.DataBind();
          GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
  
          for (int i = 0; i &lt; GridView1.Rows.Count; i++)
          {
              GridViewRow row = GridView1.Rows[i];
              //Change Color back to white
              row.BackColor = System.Drawing.Color.White;
              //Apply text style to each Row
              row.Attributes.Add("class", "textmode");
          }
          GridView1.RenderControl(hw);
          //style to format numbers to string
          string style = @"&lt;style&gt; .textmode { mso-number-format:\@; } &lt;/style&gt;";
          Response.Write(style);
          Response.Output.Write(sw.ToString());
          Response.Flush();
          Response.End();
      }
  </code></pre>
  
  <p>but not working properly and on opening the file it gives following error
  "Excel cannot open the file 'ChangeRequestList[2].xlsx' because the file 
  format or file extension is not valid. Verify that the file has not been 
  corrupted and that the file extension matches the format of the file"</p>
  
  <p>Can anyone help me?</p>
  
  <p>Thanks</p>