0

使用NReco ExcelPivotTableWriter创建数据透视表时出现系统内存不足异常

 public void Write(PivotTable pvtTbl)
        {
            var tbl = getPivotDataAsTable(pvtTbl.PivotData);
            var rangePivotTable = wsData.Cells["A1"].LoadFromDataTable(tbl, false);

            var pivotTable = ws.PivotTables.Add(
                    ws.Cells[1, 1],
                    rangePivotTable, "pvtTable");

            foreach (var rowDim in pvtTbl.Rows)
                pivotTable.RowFields.Add(pivotTable.Fields[rowDim]);
            foreach (var colDim in pvtTbl.Columns)
                pivotTable.ColumnFields.Add(pivotTable.Fields[colDim]);

            pivotTable.ColumGrandTotals = false;
            pivotTable.DataOnRows = false;
            pivotTable.ColumGrandTotals = false;            
            pivotTable.RowGrandTotals = false;


            if (pvtTbl.PivotData.AggregatorFactory is CompositeAggregatorFactory)
            {                
                var aggrFactories = ((CompositeAggregatorFactory)pvtTbl.PivotData.AggregatorFactory).Factories;
                for (int i = 0; i < aggrFactories.Length; i++)
                {
                    var dt = pivotTable.DataFields.Add(pivotTable.Fields[String.Format("value_{0}", i)]);
                    dt.Function = SuggestFunction(aggrFactories[i]);

                    string columnName = "";
                    if (dt.Function == OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Sum)
                          columnName = ((NReco.PivotData.SumAggregatorFactory)aggrFactories[i]).Field;
                    else if(dt.Function == OfficeOpenXml.Table.PivotTable.DataFieldFunctions.Average)
                         columnName = ((NReco.PivotData.AverageAggregatorFactory)aggrFactories[i]).Field;

                    if (columnNames.ContainsKey(columnName))
                        dt.Name = columnNames[columnName].ToString();
                    else
                        dt.Name = aggrFactories[i].ToString();                             
                }
            }
            else
            {
                pivotTable.DataFields.Add(pivotTable.Fields["value"]).Function = SuggestFunction(pvtTbl.PivotData.AggregatorFactory);
            }

        }

创建 rangePivotTable 时发生错误

   var rangePivotTable = wsData.Cells["A1"].LoadFromDataTable(tbl, false);

LazyTotal 模式为真

   var ordersPvtData = new PivotData(dimentionsArray, composite, true);

数据集有 200k 行。我认为这并不过分。我在 Windows 10 上有 8 GB 内存。NReco 是免费版本。有什么解决办法吗?

4

2 回答 2

0

您应该能够很容易地切开 200k 行。像这样试试。. .

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\your_path_here\SampleFile.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "Data Source";
Worksheet sheet2 = workbook.CreateEmptySheet();
sheet2.Name = "Pivot Table";
CellRange dataRange = sheet.Range["A1:G200000"];
PivotCache cache = workbook.PivotCaches.Add(dataRange);
PivotTable pt = sheet2.PivotTables.Add("Pivot Table", sheet.Range["A1"], cache);
var r1 = pt.PivotFields["Vendor No"];
r1.Axis = AxisTypes.Row;
pt.Options.RowHeaderCaption = "Vendor No";

var r2 = pt.PivotFields["Description"];
r2.Axis = AxisTypes.Row;
pt.DataFields.Add(pt.PivotFields["OnHand"], "SUM of OnHand", SubtotalTypes.Sum);
pt.DataFields.Add(pt.PivotFields["OnOrder"], "SUM of OnOrder", SubtotalTypes.Sum);
pt.DataFields.Add(pt.PivotFields["ListPrice"], "Average of ListPrice", SubtotalTypes.Average);

pt.BuiltInStyle = PivotBuiltInStyles.PivotStyleMedium12;
workbook.SaveToFile("PivotTable.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("PivotTable.xlsx");
于 2016-12-28T18:44:35.843 回答
0

8G may not be enough physical memory depending upon how large each of the 200K rows are and the memory consumption of the other applications running on your system.

Before you run this program, start the Windows Task Manager and click on the Performance tab.

enter image description here

Note the Available and Free Memory values. Then run your program and watch how the memory is consumed. If your program does consume all of your available memory, then your options are...

  1. Free up more memory by removing other applications that consume memory.
  2. Add more physical memory to your system.
  3. Modify your program to make it more memory efficient. (this includes removal of memory leaks)
  4. Some combination of the prior three options.
于 2016-10-14T14:48:38.297 回答