Q:

Dot Net : Exporting content from gridview to excel file

Hi need some help in exporting from gridview to excel file. 
The following code I have use actually export the whole web page instead of content from the gridview.  I would want to only export content from the gridview itself.  Please help, thanks~
string attachment = "attachment; filename=data.xls";
            Response. ClearContent();
            Response. AddHeader("content-disposition", attachment);
            Response. ContentType = "application/vnd.ms-excel";
            string tab = "";
            foreach (DataColumn dc in gvSessionList. Columns)
            {
                Response. Write(tab + dc. ColumnName);
                tab = "\t";
            }
            Response. Write("\n");
            int i;
            foreach (DataRow dr in gvSessionList. Rows)
            {
                tab = "";
                for (i = 0; i < gvSessionList. Columns. Count; i++)
                {
                    Response. Write(tab + dr[i].ToString());
                    tab = "\t";
                }
                Response. Write("\n");
            }
            Response. End();

A:

Normally a grid view displays information from a data table such as the below code. 
    GridView. DataSource = DataTableResult;
    GridView. DataBind();

One can save the data table in a session variable such as
    Session["ExportDataTableResult"] = DataTableResult;

Then when the user clicks the Export to Excel button that method can take the stored data table and generate the excel file
    if(Session["ExportDataTableResult"] ! = null)
     {
      ExportToExcel((System. Data. Datatable)Session["ExportDataTableResult"])
     }

For the method that takes a data table and write into excel file i believe one can find examples of it in the internet. Hope i have given you a general idea of one way of exporting from gridview to excel file.