【问题标题】:An exception of type 'System.OutOfMemoryException'“System.OutOfMemoryException”类型的异常
【发布时间】:2016-06-11 18:04:47
【问题描述】:

我需要将数据导出为 pdf 文件, 但我有一个类型的例外

System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code

这是我的代码

 using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView ExportGrid = new GridView();
            ExportGrid.AllowPaging = false;
            List<Tayf.Models.Product> DataLst = new List<Tayf.Models.Product>();

            DataLst = HttpContext.Current.Cache["productcache"] as List<Tayf.Models.Product>;

            ExportGrid.DataSource = DataLst;//DataSet1 has all values without paging
            ExportGrid.AllowPaging = false;

            ExportGrid.DataBind();

            ExportGrid.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
            pdfDoc.Close();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
    }

我的数据非常庞大,超过 100 万行,我需要将所有数据导出到一个文件中

【问题讨论】:

  • “未处理”。使用 try catch 并从异常中打印出堆栈跟踪。
  • @MillieSmith 会解决问题吗!?它会忽略错误
  • 它将帮助您找出问题所在,以便您解决问题

标签: c# asp.net pdf io


【解决方案1】:

由于你的数据这么大,也许你应该把你的数据分成很多部分,分别写到pdf中。

【讨论】:

  • @Nesreenadly 我不熟悉您在代码中使用的工具,但也许您可以逐行编写文件?抱歉,这只是我的简单想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 2013-01-14
  • 2013-08-28
  • 2015-07-18
相关资源
最近更新 更多