Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I am having multiple pages in gridview. but after export, only first page is getting exported. What to do for exporting all pages in excel?
...............
C#
protected void btnExport_Click(object sender, EventArgs e)
    {
        if (lblMsg.Visible == true)
        {
            Response.Redirect("~/Reports.aspx");
        }


        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment;filename=FirstExport.xls");
        Response.ContentType = "application/excel";
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        gridName.AllowPaging = false;
        gridName.DataBind();

        gridName.HeaderRow.Style.Add("background-color", "#FFFFFF");
        foreach (TableCell tableCell in gridName.HeaderRow.Cells)
        {
            tableCell.Style["background-color"] = "#A55129";
        }
        foreach (GridViewRow gridViewRow in gridName.Rows)
        {
            gridViewRow.BackColor = System.Drawing.Color.White;
            foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)
            {
                gridViewRowTableCell.Style["background-color"] = "#FFF7E7";
            }
        }

        gridName.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();
        gridName.AllowPaging = true;
    }
Posted
Updated 12-May-15 18:30pm
v2
Comments
Sergey Alexandrovich Kryukov 13-May-15 0:41am    
I failed to find a single line of code where you do the export at least one line. Maybe you are exporting to HTML, but where is Excel? Your content type string means just nothing.
—SA

1 solution

Bind your Gridview from Database Instead of this
C#
gridName.DataBind();


Hope this will help you :)
 
Share this answer
 
Comments
Member 11304660 13-May-15 0:48am    
how to bind gridview from database?
King Fisher 13-May-15 0:51am    
How did you got the Data's in Gridview?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900