Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
Microsoft.Office.Interop.Excel.XlPivotFilterType

to select top 2 records. Here is my code:
C#
Microsoft.Office.Interop.Excel.PivotCache tpc = book.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlDatabase, xCel.Range["RawDataRange"] );
                    var dataSheet = sheet;
                    xCel.Calculate();


                    //Summary Tab
                    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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900