【问题标题】:Export to excel throwing System.OutOfMemoryException导出到 excel 抛出 System.OutOfMemoryException
【发布时间】:2013-09-28 02:06:29
【问题描述】:

尝试将数据表从内存流写入输出流时,网页在尝试将 excel 文件传递​​给用户时抛出 System.OutOfMemoryException。我使用封闭式 XML 在 Excel 中保存文件,数据表大约有 40K 行和 150 列大,主要包含小数,导出的文件通常为 10MB 或更大。在导出到 excel 时破解大型数据集的建议技巧是什么?

这是来自封闭 XML 的代码,我正在使用 http://closedxml.codeplex.com/wikipage?title=How%20do%20I%20deliver%20an%20Excel%20file%20in%20ASP.NET%3f&referringTitle=Documentation

            public HttpResponseMessage Get()
            {
            // Create the workbook
            var workbook = new XLWorkbook();
            Datatable dt = getDataTable();
            workbook.Worksheets.Add(dt);

            // Prepare the response
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            var memoryStream = new MemoryStream(); // If I put this in a 'using' construct, I never get the response back in a browser.
            workbook.SaveAs(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin); // Seem to have to manually rewind stream before applying it to the content.
            response.Content = new StreamContent(memoryStream);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "HelloWorld.xlsx" };
            return response;
            }

【问题讨论】:

    标签: c# asp.net out-of-memory export-to-excel httpresponse


    【解决方案1】:

    我偶然发现了这个链接OpenXML libraries (alternatives to ClosedXML) 而EPPlus https://epplus.codeplex.com/ 在将大量数据导出到Excel 时比ClosedXML 工作得更好。 至少不再出现“OutOfMemory”异常,因为 EPPlus 似乎避开了 Memory Streams,尽管我仍然想知道他们是如何做到这一点的,甚至想了解 Closed XML 和 EPPlus 之间的区别。

    【讨论】:

      【解决方案2】:

      您好,您可以通过以下类型来避免内存溢出异常。

      1. 使用 reflation 方法会减少内存并获得良好的性能。
      2. 使用下面的代码 sn-p 仅将值加载到 Excel 工作表。

      代码 sn-p[C#]:

      ExcelEngine excelEngine = new ExcelEngine();
                  IApplication application = excelEngine.Excel;
                  IWorkbook workbook = application.Workbooks.Create(1); //We are using single workbook
                  IWorksheet sheet = workbook.Worksheets[0]; //In this case we are exporting to single ExcelSheet so we marked Worksheets as 0 
                  for (int i = 0; i < grid.Model.RowCount; i++)
                  {
                      //Setting Excel cell height based on Grid Cell height
                      sheet.SetRowHeightInPixels(i + 1, set heigth here);
                      for (int j = 0; j < ColumnCount; j++)
                      {
                          int width = Convert.ToInt32(ColumnWidths[j]); //Getting Grid Cell column width
                          sheet.SetColumnWidthInPixels(j + 1, width); //Setting Width for Excel cell
                          sheet.Range[i + 1, j + 1].Text = dt value here;
                      }
                  }
      

      【讨论】:

      • 您能否发布一些有用的链接来解释 Reflation Method?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多