C# OpenXML内存泄漏

C# OpenXML内存泄漏,c#,memory-leaks,openxml,C#,Memory Leaks,Openxml,我使用OpenXML将数据表导出到Excel文件 例如,我使用一个大约有250000行和10列的表。将其导出到Excel时,大约需要1.2GB的内存,有时会抛出OutOfMemoryException 我的问题有什么解决办法吗 这是我的密码 ExcelDocument excelDocument = new ExcelDocument(); excelDocument.CreatePackage(exportFile); //populate t

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

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

我的问题有什么解决办法吗

这是我的密码

        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));
            }
        }
ExcelDocument ExcelDocument=新的ExcelDocument();
excelDocument.CreatePackage(导出文件);
//将数据填充到电子表格中
使用(电子表格)文档电子表格=
电子表格文档。打开(导出文件,true))
{
WorkbookPart工作簿=电子表格。WorkbookPart;
//创建对图纸1的引用
WorksheetPart工作表=工作簿.WorksheetParts.Last();
SheetData data=worksheet.worksheet.GetFirstChild();
//将列名添加到第一行
行标题=新行();
header.RowIndex=(UInt32)5;
foreach(table.Columns中的DataColumn列)
{
样式表样式表=workbook.workbookstypespart.Stylesheet;
//构建格式化的标题样式
UINT32值标题索引=
创建字体(
样式表,
“Arial”,
9,
是的,
系统。图纸。颜色。白色);
//设置背景色样式
UINT32值头刻度线=
创建填充(
样式表,
系统。图纸。颜色。灰色);
//通过组合字体/背景创建单元格样式
UINT32值头样式索引=
createCellFormat(
样式表,
校长索引,
人头费指数,
无效);
单元格头单元格=createTextCell(
表.Columns.IndexOf(column)+1,
5.
column.ColumnName,headerStyleIndex);
header.AppendChild(headerCell);
}
data.AppendChild(表头);
//循环遍历每个数据行
数据行内容行;
for(int i=0;i
问题是在处理电子表格文档时花费了太多的时间,似乎在处理时会将所有数据刷新到excel表格中,因此我创建了excel表格,每个文件只生成30000条记录,并强制运行垃圾收集器,解决了我的问题