asp.net mvc内存不足错误

本文关键字:错误 内存不足 mvc net asp | 更新日期: 2023-09-27 18:29:33

我正试图让这段代码返回excel文件,但我遇到了内存不足的异常。知道我该怎么解决吗?很明显,里面有很多记录。它在其他地方工作

            GridView gv = new GridView();
            List<ExportReportInvoice> list = _commonSer.ExportReportInvoice(data.OrderByDescending(m=>m.MediaPlanBillingInvoiceId));
            gv.DataSource = list;
            gv.DataBind();
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment; filename=Sheet.xls");
            Response.ContentType = "application/ms-excel";
            Response.Charset = "";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

       public List<ExportReportInvoice> ExportReportInvoice(IOrderedQueryable<MediaPlanRO> roslist)
       {
        if (roslist.Count() > 0)
        {
            List<ExportReportInvoice> repo = new List<ExportReportInvoice> { };
            foreach (var r in roslist)
            {
                ExportReportInvoice rep = new ExportReportInvoice();
                rep.RONo = r.RONo;
                rep.InvStatus = r.IsInvoiceGenerated == true ? "Generated" : "Pending";
                rep.CampaignNo = r.MediaPlan.Campaign.CampaignId;
                rep.InsDate = string.Format("{0:dd MMM yyyy}", r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.StartDate);
                rep.Client = r.MediaPlan.Campaign.Company.Name;

                rep.Sublient = C_SubClients;
                rep.Publication = r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.Company.Name;
                rep.Size = Math.Round(r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.Width * r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.Height, 0)
                                            + "(" + Math.Round(r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.Width, 0) + "x" + Math.Round(r.MediaPlanROPrints.FirstOrDefault().MediaPlanPrint.Height, 0) + ")";

                rep.Rate = r.MediaPlanROPrints.Sum(s => s.MediaPlanPrint.RoRate).ConvertToDecimal();
                rep.RoAmount = r.MediaPlanROPrints.Sum(s => s.MediaPlanPrint.Amount).ConvertToDecimal();
                rep.EstAmount = r.MediaPlanROPrints.Sum(s => s.MediaPlanPrint.EstimatedAmount).ConvertToDecimal();
                repo.Add(rep);
            }
            return repo;
        }
        else return null;
    }

asp.net mvc内存不足错误

您可以将输出直接写入响应流,而不是将其加载到内存中(使用此StringWriter):

HtmlTextWriter htw = new HtmlTextWriter(Response.Output);
gv.RenderControl(htw);
Response.Flush();
Response.End();

在ASP.NET MVC应用程序中使用GridView控件也很好,如何调用它,嗯,找不到合适的单词(18+)。