导出Excel时System.OutOfMemory

时间:2018-04-17 22:04:57

标签: c# out-of-memory export-to-excel

我需要将文件导出为ex​​cel,但是当我使用此代码执行此操作时,请在此行中返回错误“Exception.SystemOutOfMemory”:wb.Worksheets.Add(dt,“Relatorio”);

  using (XLWorkbook wb = new XLWorkbook())
        {

            string db = Request.QueryString["DB"].ToString().Replace("-", "_");
            string de = Request.QueryString["DB"].ToString().Replace("-", "_");


            wb.Worksheets.Add(dt, "Relatorio");
            wb.Worksheet(1).Range("A1:O1").Style.Fill.BackgroundColor = XLColor.Gainsboro;
            wb.Worksheet(1).Range("A1:O1").Style.Font.FontColor = XLColor.DimGray;

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=InventoryReport___" + db + "___" +
                               de + "___" + DateTime.Now.Day + "_" + DateTime.Now.Month + "_" +
                               DateTime.Now.Year + "_" + DateTime.Now.Hour + "_" + DateTime.Now.Minute + "_" +
                               DateTime.Now.Second + ".xlsx");

            using (MemoryStream MyMemoryStream = new MemoryStream())
            {

                wb.SaveAs(MyMemoryStream);
                MyMemoryStream.WriteTo(Response.OutputStream);
                Response.Flush();
                Response.End();
            }

        }

0 个答案:

没有答案