Export gridview with controls to excel sheet in ASP.Net

Last Reply on Jun 09, 2016 06:59 AM By dharmendr

Posted on Jun 08, 2016 08:47 AM

as i write the code as in the following  link 

http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx


but the response.flush() not appear any thing 

 

please tell me the what's wrong on my code

 

thanks

You are viewing reply posted by: dharmendr on Jun 09, 2016 06:59 AM.
Posted on Jun 09, 2016 06:59 AM Modified on one year ago

Hi mehad,

I have created sample using your code.

HTML

<div>
    <asp:Button ID="btnExport_Page" CssClass="add" runat="server" OnClick="btnExport_Page_Click"
        Text="export page"></asp:Button>
</div>
<asp:GridView runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerId"
    AllowPaging="true" PageSize="5" ID="gvProcessReport" DataSourceID="SqlDataSource1" OnPageIndexChanging = "OnPaging">
    <Columns>
        <asp:TemplateField HeaderText="CustomerId">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("CustomerId") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                <asp:TextBox ID="txtMessage" TextMode="MultiLine" runat="server" Enabled="false"
                    Text='<%# Eval("Name") %>' Width="97%"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="200px" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Country">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("Country") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    SelectCommand="SELECT * FROM [Customers]"></asp:SqlDataSource>

Code

public void ExportOnlyOnePage()
{
    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);
        gvProcessReport.AllowPaging = false;
        gvProcessReport.DataBind();
        gvProcessReport.HeaderRow.BackColor = System.Drawing.Color.White;
        foreach (TableCell cell in gvProcessReport.HeaderRow.Cells)
        {
            cell.BackColor = gvProcessReport.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in gvProcessReport.Rows)
        {
            row.BackColor = System.Drawing.Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = gvProcessReport.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = gvProcessReport.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }
        gvProcessReport.RenderControl(hw);
        string style = @"<style> .textmode { } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */
}

protected void OnPaging(object sender, GridViewPageEventArgs e)
{
    GridView gv = (GridView)gvProcessReport;
    gv.PageIndex = e.NewPageIndex;
    gv.DataBind();
}

protected void btnExport_Page_Click(object sender, EventArgs e)
{
    ExportOnlyOnePage();
}

Screenshot