簡體   English   中英

導出到Excel時出現網格綁定錯誤

[英]Grid binding error while exporting to excel

我有一個具有75000條記錄的GridView。 數據將在幾天內增加。 使用分頁時,在UI中填充時沒有問題。 現在,在導出為ex​​cel時,所有博客都建議刪除分頁,然后再次加載網格以進行導出。 但是在此過程中,數據綁定失敗,並出現內存不足異常。 請幫忙。 我什至嘗試加載到數據表並重新加載到新的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;
        }
    }
}

具有XLS擴展名的HTML文件不是真正的MS Excel文件。 MS Excel僅知道如何讀取它們並顯示數據。

保存大型HTML文件會導致較高的內存分配,這很耗時。

您應使用EasyXLS之類的Excel庫來保存xls或xlsx Excel文件,並具有更好的內存管理。

檢查以下鏈接以獲取指導:
用C#將Gridview導出到Excel

用C#導出大型Excel文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM