导出到Excel时抛出System.OutOfMemoryException异常

3
一份网页在尝试将数据表从内存流写入输出流时,抛出了System.OutOfMemoryException异常,而我正在使用Closed XML将文件保存为Excel格式,数据表大约有40K行和150列,主要包含小数,导出的文件通常大于10MB。在导出到Excel时,有什么建议的技巧可以解决大型数据集的问题吗?这是我使用的来自Closed 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;
            }
2个回答

4
我偶然发现了这个链接: OpenXML libraries (alternatives to ClosedXML)。当导出大量数据到Excel时,EPPlus https://epplus.codeplex.com/ 比ClosedXML更好用。至少不会再出现“OutOfMemory”的异常,因为EPPlus似乎避开了内存流,虽然我仍然对他们是如何实现这一点感兴趣,甚至想了解Closed XML和EPPlus之间的区别。

3

你可以通过以下方法避免内存溢出异常:

  1. 使用反射方法可以减少内存占用并提高性能。
  2. 使用以下代码片段仅将值加载到 Excel 工作表中。

代码片段 [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;
                }
            }

你能否发布一些有用的链接,解释一下反射法? - Anusha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接