系统内存不足异常-OpenXML

时间:2019-06-11 18:06:49

标签: c# .net wpf c#-4.0 openxml

我需要将大量数据导出到Excel。数据源自对象列表。数据的大小各不相同,但最多可以有7000列和20,000行。

我正在具有24 gig公羊的笔记本电脑上使用Visual Studio 2017中的WPF。

下面的代码生成系统内存不足异常。如何更改下面的代码以处理将大数据导出到excel?

   public static void LargeExportTest(List<ExcelData> arrExport, string sDefaultPath)
    {
        string filename = sDefaultPath + "\\" + Path.GetRandomFileName() + ".xlsx";
        UInt32 sheetId = 1;
        try
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
            {
                //this list of attributes will be used when writing a start element
                List<OpenXmlAttribute> attributes;
                OpenXmlWriter writer;

                document.AddWorkbookPart();
                WorksheetPart workSheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();

                writer = OpenXmlWriter.Create(workSheetPart);
                writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Worksheet());
                writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                int batchCount = 0;
                for (int rowNum = 0; rowNum <= arrExport.Count-1; ++rowNum)
                {
                    int u = Convert.ToInt32(rowNum.ToString());

                    batchCount++;
                    //create a new list of attributes
                    attributes = new List<OpenXmlAttribute>();
                    // add the row index attribute to the list
                    attributes.Add(new OpenXmlAttribute("r", null, (rowNum + 1).ToString()));

                    //write the row start element with the row index attribute
                    writer.WriteStartElement(new Row(), attributes);
                    var MaxCol = arrExport[u].ColumnData.Max(x => x.ColumnNumber);

                    attributes = new List<OpenXmlAttribute>();
                    // add data type attribute - in this case inline string (you might want to look at the shared strings table)
                    attributes.Add(new OpenXmlAttribute("t", null, "str"));
                    //add the cell reference attribute
                    attributes.Add(new OpenXmlAttribute("r", "", string.Format(arrExport[u].Name)));

                    //write the cell start element with the type and reference attributes
                    writer.WriteStartElement(new Cell(), attributes);
                    //write the cell value
                    writer.WriteElement(new CellValue(string.Format(arrExport[u].Name)));

                    // write the end cell element
                    writer.WriteEndElement();

                    foreach (var col in arrExport[u].ColumnData)
                    {
                        //reset the list of attributes
                        attributes = new List<OpenXmlAttribute>();
                        // add data type attribute - in this case inline string (you might want to look at the shared strings table)
                        attributes.Add(new OpenXmlAttribute("t", null, "str"));
                        //add the cell reference attribute
                        attributes.Add(new OpenXmlAttribute("r", "", string.Format("{0}{1}", GetColumnName(col.ColumnNumber + 1), (rowNum + 1))));

                        //write the cell start element with the type and reference attributes
                        writer.WriteStartElement(new Cell(), attributes);
                        //write the cell value
                        writer.WriteElement(new CellValue(col.ColumnData.ToString()));

                        // write the end cell element
                        writer.WriteEndElement();
                    }

                    // write the end row element
                    writer.WriteEndElement();

                    if(batchCount > 150)
                    {
                        // write the end SheetData element
                        writer.WriteEndElement();
                        // write the end Worksheet element
                        writer.WriteEndElement();
                      //  writer.Close();



                        //writer.WriteElement(new Sheet()
                        //{
                        //    Name = "Large Sheet" + sheetId.ToString(),
                        //    SheetId = sheetId,
                        //    Id = document.WorkbookPart.GetIdOfPart(workSheetPart)
                        //});
                        sheetId++;

                        //////////////////////////////////////

                        writer = OpenXmlWriter.Create(workSheetPart);
                        writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Worksheet());
                        writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                        batchCount = 0;
                    }
                }

                // write the end SheetData element
                writer.WriteEndElement();
                // write the end Worksheet element
                writer.WriteEndElement();
                writer.Close();

                writer = OpenXmlWriter.Create(document.WorkbookPart);
                writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Workbook());
                writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Sheets());

                writer.WriteElement(new Sheet()
                {
                    Name = "Large Sheet" + sheetId.ToString(),
                    SheetId = sheetId,
                    Id = document.WorkbookPart.GetIdOfPart(workSheetPart)
                });

                // End Sheets
                writer.WriteEndElement();
                // End Workbook
                writer.WriteEndElement();

                writer.Close();

                document.Close();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我希望生成的Excel文件没有错误,但是会出现内存不足异常

编辑:如果处理了150多个行,我尝试更改代码以尝试创建新的工作表。它没有创建新的工作表,因此代码无法正常工作。 OpenXml的新功能,并且不确定如何添加新的工作表。有建议吗?

0 个答案:

没有答案