0

OpenXML を使用して、データ テーブルを Excel ファイルにエクスポートします。

たとえば、約 250,000 行と約 10 列のテーブルを使用します。それを Excel にエクスポートすると、それを行うために約 1.2 GB のメモリが必要になり、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));
            }
        }
4

1 に答える 1

1

問題は、SpreadsheetDocument を破棄するときに時間がかかりすぎることでした。破棄するときにすべてのデータを Excel シートにフラッシュするようです。そのため、作成された Excel シートを吐き出し、ファイルごとに 30,000 レコードしか作成せず、Garbage Collector を強制的に実行して解決しました。問題

于 2012-11-18T08:13:24.857 に答える