2016-10-14 23 views
0

我m到處內存異常的系統停止,同時用NReco ExcelPivotTableWriter樞軸Excel中隨着NReco,內存異常的系統停止

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); 
      } 

     } 

錯誤創建透視表occures同時創造rangePivotTable

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

的LazyTotal模式爲真

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

數據集有200k行。這不是我想的太多。我有8 gb RAM在Windows 10上。 NReco是免費版本。 任何解決方案?這取決於多大每個200K行的是,你的系統上運行的其它應用程序的內存消耗

+0

首先確保您的.net程序作爲x64應用程序執行並可以使用所有可用內存。然後,嘗試減少爲PivotData類配置的維數。 ExcelPivotTableWriter使用EPPlus庫導出到Excel數據透視表,它實際上可以消耗大量RAM,具體取決於數據集的大小。 –

+0

我的應用程序作爲AnyCpu運行 –

回答

0

8G可能沒有足夠的物理內存。

在運行此程序之前,請啓動Windows任務管理器並單擊性能選項卡。

enter image description here

注意的可用內存和可用內存的值。然後運行你的程序並觀察內存如何被消耗。如果您的程序確實消耗了所有可用內存,那麼您的選擇是...

  1. 通過刪除消耗內存的其他應用程序釋放更多的內存。
  2. 爲您的系統添加更多物理內存。
  3. 修改您的程序以提高內存使用效率。 (這包括清除內存泄漏)
  4. 先前三個選項的某些組合。
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");