如何将所有网格数据导出到Excel?

时间:2015-09-16 09:45:00

标签: asp.net gridview export

当我添加下面的代码工作正常但它只从一个页面中的Gridview导出数据而不是所有数据,如果它启用分页,<​​/ p>

请给我你的建议:)

    protected void BtnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Off-Board.xls");
        Response.ContentType = "application/excel";

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GridView_Report.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();

    }
enter code here

2 个答案:

答案 0 :(得分:2)

After Export
GridView_Report.AlloPaging=True;
GrridView_Report.DataSource=Your_datasource;

GridView_Report.DataBind();

答案 1 :(得分:1)

尝试包括

GridView_Report.AllowPaging=False;
GridView_Report.DataSource=your_datasource;
GridView_Report.DataBind();

并在导出后设置

GridView_Report.AllowPaging=True;
GridView_Report.DataSource=your_datasource;
GridView_Report.DataBind();

在你的情况下

protected void BtnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Off-Board.xls");
        Response.ContentType = "application/excel";

        //here

GridView_Report.AllowPaging=False;
    GridView_Report.DataSource=your_datasource;
    GridView_Report.DataBind();

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        GridView_Report.RenderControl(htmlTextWriter);
        Response.Write(stringWriter.ToString());
        Response.End();

GridView_Report.AllowPaging=True;
    GridView_Report.DataSource=your_datasource;
    GridView_Report.DataBind();

    }