如何使用分页将gridview导出到Excel工作表

时间:2012-08-17 06:19:44

标签: c# asp.net export-to-excel

我尝试使用radiobuttonlist让用户选择是否要将gridview的当前页面或整个gridview导出到Excel工作表,但只有当前页面的工作位置才能导出整个gridview到excel表,它只显示标题行。这里有什么问题,是否与radiobuttonlist的selectedindex有关?

protected void btnExport_Click(object sender, EventArgs e)
{
    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  the user wants all rows in the current page, set pagesize and rebind
        this.GridView1.PageSize = 10;
        this.GridView1.DataBind();
    }
    else if (this.RadioButtonList1.SelectedIndex == 2)
    {
        //  the user wants all rows exported, have to turn off paging and rebind
        this.GridView1.AllowPaging = false;
        this.GridView1.DataBind();
    }
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}

1 个答案:

答案 0 :(得分:1)

只是一个想法

protected void btnExport_Click(object sender, EventArgs e)
{
    var collection = GetTheDataSource(); // Get the source.

    if (this.RadioButtonList1.SelectedIndex == 1)
    {
        //  take first 10 items from the collection     
        collection = collection.Take(10); 
    }
    //set the grid source followed by binding it
    this.GridView1.DataSource = collection;
    this.GridView1.DataBind();

    //Export the grid record to excel
    GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1);
}