导出到excel时出现网格绑定错误

时间:2016-12-22 21:15:58

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

我有一个包含75000条记录的GridView。数据将在几天内增加。我在使用分页时在UI中填充时没有问题。现在,在导出到excel时,所有博客都建议删除分页,然后再次加载网格以导出。但在此过程中,数据绑定失败并出现内存不足异常。请帮忙。我甚至尝试加载到datatable并重新加载到新的gridview。

(在下面添加了我的代码,目前这只是多次循环网格中的最后一页)

try
        {
            GrdReport.AllowPaging = false;
            LoadReportData();
            int a = GrdReport.PageIndex;
            if (GrdReport.PageCount <= 650)
            {
                DataTable dt = new DataTable();
                for (int i = 0; i < GrdReport.PageCount; i++)
                {
                    GrdReport.PageIndex = i;
                    //GrdReport.SetPageIndex(a);

                    if (i == 0)
                    {
                        for (int k = 0; k < GrdReport.HeaderRow.Cells.Count; k++)
                        {

                            if (GrdReport.HeaderRow.Cells[k].HasControls())
                            {
                                                                    if (GrdReport.HeaderRow.Cells[k].Controls[0] is LinkButton)
                                {
                                    LinkButton headerControl = GrdReport.HeaderRow.Cells[k].Controls[0] as LinkButton;
                                    string headerName = headerControl.Text;
                                    dt.Columns.Add(headerName);
                                }

                            }
                        }
                    }

                    foreach (GridViewRow row in GrdReport.Rows)
                    {

                        dt.Rows.Add();
                        for (int j = 0; j < row.Cells.Count; j++)
                        {
                            dt.Rows[dt.Rows.Count - 1][j] = row.Cells[j].Text;
                        }
                    }
                }
                int x = dt.Rows.Count;
                int y = dt.Columns.Count;
                GrdReport.SetPageIndex(a);

                Session["New"] = dt;
                HttpResponse Response = HttpContext.Current.Response;
                Response.Redirect("ExportToExcelHandler.ashx?gv=" + Session["New"], false);

            }

            else
            {
                lblErr.Text = "Result exceeds 65000 records. Please modify search criteria to reduce records.";
                lblErr.Visible = true;
            }
        }

这是处理程序中的代码:

public class ExportToExcelHandler : System.Web.UI.Page, IHttpHandler, IRequiresSessionState
{

    public new void ProcessRequest(HttpContext context)
    {
        GridView grid = new GridView();
        this.EnableViewState = false;           
        grid.DataSource = (DataTable)HttpContext.Current.Session["New"];
        grid.DataBind();
        HttpResponse Response = HttpContext.Current.Response;
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Results.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter StringWriter = new StringWriter();          
        HtmlTextWriter HtmlTextWriter = new System.Web.UI.HtmlTextWriter(Response.Output);            
        grid.RenderControl(HtmlTextWriter);
        Response.End();
    }

    public new bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

具有XLS扩展名的HTML文件不是真正的MS Excel文件。 MS Excel只知道如何阅读和显示数据。

保存大型HTML文件会导致内存分配过多,而且耗时。

您应该使用像http://lgallardo.com/2011/06/23/sentencias-preparadas-de-mysql-en-c-ejemplo-completo/这样的Excel库来保存xls或xlsx Excel文件,并且具有更好的内存管理。

检查以下链接以获取指示:
EasyXLS

Export Gridview to Excel in C#