使用 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);
数据集有200万行。我认为这不是太多。我在窗户10上有8 gb ram。 NReco是免费版。 有解决方案吗
答案 0 :(得分:0)
8G的物理内存可能不够,具体取决于每个200K行的大小以及系统上运行的其他应用程序的内存消耗。
在运行此程序之前,请启动Windows任务管理器,然后单击“性能”选项卡。
请注意可用和可用内存值。然后运行程序并观察内存的消耗情况。如果您的程序确实消耗了所有可用内存,那么您的选项就是......
答案 1 :(得分: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");