简体   繁体   English

如何将Gridview行导出到Excel工作表?

[英]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 我的任务是将gridview导出到excel工作表,因为我可以导出当前页面或前100行或所有页面。它与当前页面很好地工作,但其他页面却不工作

 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 试试这个,它适用于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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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