'System.OutOfMemoryException'类型的例外

时间:2016-06-11 18:04:47

标签: c# asp.net pdf io

我需要将数据导出到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万行非常庞大,我需要将所有数据导出到一个文件中

1 个答案:

答案 0 :(得分:0)

由于您的数据如此之大,或许您应该将数据分成多个部分并将它们分别写入pdf。