title: how to export data from gridview to excel 2003,2007,2010 without warning message
body: <p>I am trying to export data from gridview to excel. I have office 2010 installed on my pc. When i am trying to open excel file it gives me error i.e. "the file you are trying to open is in a different format than specified by the file extension c#".</p>
  
  <p>My Code for exporting gridview:</p>
  
  <pre><code>Protected Sub btnexptoexcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnexptoexcel.Click
      Try
          Response.Clear()
          Response.Buffer = True
          Response.AddHeader("content-disposition", "attachment;filename=Complaint_Details.xls")
          Response.Charset = ""
          Response.ContentType = "application/ms-excel"
          Using sw As New StringWriter()
              Dim hw As New HtmlTextWriter(sw)
  
              grd_ComplaintDetails.AllowPaging = False
  
  
              grd_ComplaintDetails.HeaderRow.BackColor = Color.White
              For Each cell As TableCell In grd_ComplaintDetails.HeaderRow.Cells
                  cell.BackColor = grd_ComplaintDetails.HeaderStyle.BackColor
              Next
              For Each row As GridViewRow In grd_ComplaintDetails.Rows
                  row.BackColor = Color.White
                  For Each cell As TableCell In row.Cells
                      If row.RowIndex Mod 2 = 0 Then
                          cell.BackColor = grd_ComplaintDetails.AlternatingRowStyle.BackColor
                      Else
                          cell.BackColor = grd_ComplaintDetails.RowStyle.BackColor
                      End If
                      cell.CssClass = "textmode"
                  Next
              Next
  
              grd_ComplaintDetails.RenderControl(hw)
              Dim style As String = "&lt;style&gt; .textmode { } &lt;/style&gt;"
              Response.Write(style)
              Response.Output.Write(sw.ToString())
              Response.Flush()
              Response.[End]()
          End Using
  
      Catch ex As Exception
          div_Msg.InnerText = "Can not generate Excel File"
      End Try
  
  End Sub
  </code></pre>
  
  <p>My question is when i open file (in MSOffice 2003, 2007 or 2010) it shouldnt give me file extension error...
  Can you please tell me what are the changes i should made in the code???</p>