title: button to export tables to excel in asp.net
body: <p>I have few pages of tables, and I have created a button to export the table to excel. my problem is that it only export the current page of table that in at and not all the pages? can anyone help me out?</p>
  
  <pre><code>protected void ExportToExcel(object sender, EventArgs e)
      {
          Response.Clear();
          Response.Buffer = true;
          Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
          Response.Charset = "";
          Response.ContentType = "application/vnd.ms-excel";
          using (StringWriter sw = new StringWriter())
          {
              HtmlTextWriter hw = new HtmlTextWriter(sw);
  
              //To Export all pages
              GridView1.AllowPaging = false;
  
  
              GridView1.HeaderRow.BackColor = Color.White;
              foreach (TableCell cell in GridView1.HeaderRow.Cells)
              {
                  cell.BackColor = GridView1.HeaderStyle.BackColor;
              }
              foreach (GridViewRow row in GridView1.Rows)
              {
                  row.BackColor = Color.White;
                  foreach (TableCell cell in row.Cells)
                  {
                      if (row.RowIndex % 2 == 0)
                      {
                          cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                      }
                      else
                      {
                          cell.BackColor = GridView1.RowStyle.BackColor;
                      }
                      cell.CssClass = "textmode";
                  }
              }
  </code></pre>