首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenXML内存泄漏

OpenXML内存泄漏
EN

Stack Overflow用户
提问于 2012-11-13 01:01:16
回答 1查看 3K关注 0票数 0

我使用OpenXML将数据表导出到Excel文件中。

例如,我使用一个大约有250,000行和大约10列的表。在将其导出到Excel时,需要大约1.2 GB的内存,有时还会抛出OutOfMemoryException

我的问题有什么解决方案吗?

这是我的代码

代码语言:javascript
复制
        ExcelDocument excelDocument = new ExcelDocument();
        excelDocument.CreatePackage(exportFile);

        //populate the data into the spreadsheet
        using (SpreadsheetDocument spreadsheet =
            SpreadsheetDocument.Open(exportFile, true))
        {
            WorkbookPart workbook = spreadsheet.WorkbookPart;
            //create a reference to Sheet1
            WorksheetPart worksheet = workbook.WorksheetParts.Last();
            SheetData data = worksheet.Worksheet.GetFirstChild<SheetData>();

            //add column names to the first row
            Row header = new Row();
            header.RowIndex = (UInt32) 5;

            foreach (DataColumn column in table.Columns)
            {
                Stylesheet styleSheet = workbook.WorkbookStylesPart.Stylesheet;

                //build the formatted header style
                UInt32Value headerFontIndex =
                    createFont(
                        styleSheet,
                        "Arial",
                        9,
                        true,
                        System.Drawing.Color.White);
                //set the background color style
                UInt32Value headerFillIndex =
                    createFill(
                        styleSheet,
                        System.Drawing.Color.SlateGray);
                //create the cell style by combining font/background
                UInt32Value headerStyleIndex =
                    createCellFormat(
                        styleSheet,
                        headerFontIndex,
                        headerFillIndex,
                        null);
                Cell headerCell = createTextCell(
                    table.Columns.IndexOf(column) + 1,
                    5,
                    column.ColumnName, headerStyleIndex);

                header.AppendChild(headerCell);
            }
            data.AppendChild(header);

            //loop through each data row
            DataRow contentRow;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                contentRow = table.Rows[i];
                data.AppendChild(createContentRow(contentRow, i + 6));
            }
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-18 16:13:25

问题是在处理SpreadsheetDocument时,它花费了太多的时间,似乎在处理的时候会将所有的数据都刷新到excel表中,所以我吐出了excel表,每个文件只有30,000条记录,并强制垃圾收集器运行,解决了我的问题

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13348010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档