简体   繁体   中英

How to export Gridview rows to Excel sheet?

I have my task to export gridview to excel sheet , as I can export the current page or top100 rows or all pages .it worked well with current page but the others doesn't work

 protected void btnExportGrid_Click(object sender, ImageClickEventArgs e)
{
    Table table = new Table
    {
        GridLines = this.Data.GridLines
    };
    if (this.rdoBtnListExportOptions.SelectedIndex == 1)
    {
        this.Data.AllowPaging = false;
        this.Data.DataBind();
        GridViewExportUtil.Export("Customers.xls", this.Data);
    }
    else if (this.rdoBtnListExportOptions.SelectedIndex == 2)
    {
        this.Data.PageSize = 50;
        this.Data.DataBind();
        GridViewExportUtil.Export("Customers.xls", this.Data);
    }
    else
    { GridViewExportUtil.Export("Customers.xls", this.Data); }
}

Try this, it's for ASP.NET

        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

        System.IO.StringWriter sw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

        gridview.RenderControl(hw);

        Response.Write(sw.ToString());
        Response.End();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM