I want to add pivot filter on 'Account' field. I am able to achieve this using loops. But this creates performance issue. So I want to use
Microsoft.Office.Interop.Excel.XlPivotFilterType
to select top 2 records. Here is my code:
Microsoft.Office.Interop.Excel.PivotCache tpc = book.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, xCel.Range["RawDataRange"] );
var dataSheet = sheet;
xCel.Calculate();
sheet = book.Worksheets.Add();
sheet.Move(dataSheet);
Microsoft.Office.Interop.Excel.PivotTable tpt = sheet.PivotTables().Add(tpc, sheet.Range["A1"], "SUMMARY_PNL_PIVOT");
tpt.AddDataField(tpt.PivotFields("NetAssetValue"));
tpt.PivotFields("Counterparty").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;
tpt.PivotFields("Current/LongTurm").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
tpt.PivotFields("Asset/Liability").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlColumnField;
tpt.PivotFields("ForwardDate").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("FinPhys").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
try
{
tpt.PivotFields("FinPhys").CurrentPage = "Financial";
}
catch
{ }
tpt.PivotFields("Commodity").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("PricingPointTwo").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("PricingPointOne").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("TradeDate").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("Account").ClearAllFilters();
tpt.PivotFields("Account").Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;
tpt.PivotFields("Account").PivotFilters.Add(Microsoft.Office.Interop.Excel.XlPivotFilterType.xlTopCount,2);
Last line gives error HRESULT: 0x800A03EC .
Any help is appreciated. Thanks.
What I have tried:
1) Looping through records. But this creates performance issue.
2) tpt.PivotFields("Account").PivotFilters.Add(Microsoft.Office.Interop.Excel.XlPivotFilterType.xlTopCount, tpt.PivotFields("Account"), 2, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); gives the same error : HRESULT: 0x800A03EC