简体   繁体   English

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

[英]How to export gridview to excel sheet with paging

I've tried using the radiobuttonlist to let the user choose if he/she wants to export either the current page of the gridview or the whole gridview to an excel sheet, but only the current page one's working where as for exporting the whole gridview to an excel sheet, it only shows the header rows. 我尝试使用单选按钮列表来让用户选择他/她是否要将gridview的当前页面或整个gridview导出到excel工作表,但是只有当前页面可以在将整个gridview导出到的地方工作一个Excel工作表,它仅显示标题行。 What's the problem here, is it something to do with the selectedindex of the 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);
}

Just a thought 只是一个想法

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);
}

You can Use following code...Use Linq to bind specific records from Datasource to the Grid view while paging else bind whole Datasource..then use Gridview to your Export method.. This should work... 您可以使用以下代码...使用Linq将特定记录从数据源绑定到Grid视图,而分页则将整个数据源绑定..然后使用Gridview到Export方法。

protected void btnExport_Click(object sender, EventArgs e) 
        {    
              var datasource;
              if (this.RadioButtonList1.SelectedIndex == 1)     
               {         
                  int pageSize = 10; 
                  datasource= GridViewDatasourceCollection.Skip(pageSize * pageNumber).Take(pageSize);    
                  this.GridView1.DataSource= datasource;    
                  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 = datasource;         
                  this.GridView1.DataBind();     
               }     
               GridViewExportUtil.Export("ContactsInformation.xls", this.GridView1); 
           } 

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

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