Not able to add filters to pivot table in aspose.cells

@Shivammirje1998,
Please use the following code to test it:

        Workbook workbook = new Workbook("Sample.xlsx");
        Worksheet sheet = workbook.Worksheets.Add("NewSheet");
        PivotTableCollection pivotTables = sheet.PivotTables;

        // Adding a PivotTable to the worksheet
        int index = pivotTables.Add("=Masterfile!$A$1:$D$8", "A1", "PivotTable3");

        // Accessing the instance of the newly added PivotTable
        Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];

        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 0);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Page, 1);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 3);

        //Add the following code to move PivotTable.DataField to column area
        if (pivotTable.DataField != null)
        {
            pivotTable.AddFieldToArea(PivotFieldType.Column, pivotTable.DataField);
        }

        pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;
        pivotTable.DataFields[0].NumberFormat = "0.0,";
        pivotTable.DataFields[1].NumberFormat = "0.0,";

        #region sort pivot table data columns in descending order
        PivotField currRowField = pivotTable.RowFields[0];
        currRowField.IsAutoSort = true;
        currRowField.IsAscendSort = false;
        //Sort PivotField in row area via DataField named "Quantity";
        currRowField.AutoSortField = 0;
        #endregion

        #region add conditional foramts
        int formatIndex = pivotTable.PivotFormatConditions.Add();
        PivotFormatCondition pfc = pivotTable.PivotFormatConditions[formatIndex];
        FormatConditionCollection fcc = pfc.FormatConditions;
        fcc.AddArea(pivotTable.DataBodyRange);
        
        int idx = fcc.AddCondition(FormatConditionType.CellValue);
        FormatCondition fc = fcc[idx];
        fc.Formula1 = "3000";
        fc.Operator = OperatorType.GreaterOrEqual;
        fc.Style.BackgroundColor = Color.Red;
        #endregion
        
        // Saving the Excel file
        workbook.Save("sample_out.xlsx"); 

can you try it?

Hi @John.He,

Thank you for your response.
I wanted to know how can I apply the conditional formatting on only one of the data fields and not all?
Also I am not getting how sorting in descending order will be applied on data fields as we are setting ascend sort false for one of the rowfields?

@Shivammirje1998,
In the pivot table, you can’t sort the datafield directly. You need to sort rowfields according to datafields, so as to sort datafields indirectly. After rowfields are sorted in descending order according to datafields, datafields are also sorted in descending order naturally.

Please check the attachment:
result.PNG (31.0 KB)

@Shivammirje1998
About how can I apply the conditional formatting on only one of the data fields:
Please check the attachment datafield.PNG (4.4 KB)
and try it:

        Workbook workbook = new Workbook("Sample.xlsx");
        Worksheet sheet = workbook.Worksheets.Add("NewSheet");
        PivotTableCollection pivotTables = sheet.PivotTables;
        // Adding a PivotTable to the worksheet
        int index = pivotTables.Add("=Masterfile!$A$1:$D$8", "A1", "PivotTable3");

        // Accessing the instance of the newly added PivotTable
        Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];

        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 0);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Page, 1);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
        pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 3);

        //Add the following code to move PivotTable.DataField to column area
        if (pivotTable.DataField != null)
        {
            pivotTable.AddFieldToArea(PivotFieldType.Column, pivotTable.DataField);
        }

        pivotTable.PivotTableStyleType = PivotTableStyleType.PivotTableStyleLight16;
        pivotTable.DataFields[0].NumberFormat = "0.0,";
        pivotTable.DataFields[1].NumberFormat = "0.0,";

        #region sort pivot table data columns in descending order
        PivotField currRowField = pivotTable.RowFields[0];
        currRowField.IsAutoSort = true;
        currRowField.IsAscendSort = false;
        //Sort PivotField in row area via DataField named "Quantity";
        currRowField.AutoSortField = 0;
        #endregion

        #region add conditional foramts
        int formatIndex = pivotTable.PivotFormatConditions.Add();
        PivotFormatCondition pfc = pivotTable.PivotFormatConditions[formatIndex];

        //Set conditional styles only in the DataField named "Quantity"
        pfc.AddDataAreaCondition(pivotTable.DataFields[0]);
        pfc.SetConditionalAreas();

        FormatConditionCollection fcc = pfc.FormatConditions;
        int idx = fcc.AddCondition(FormatConditionType.CellValue);
        FormatCondition fc = fcc[idx];
        fc.Formula1 = "3000";
        fc.Operator = OperatorType.GreaterOrEqual;
        fc.Style.BackgroundColor = Color.Red;
        #endregion

        // Saving the Excel file
        workbook.Save("sample_out.xlsx");

Hi @John.He,
Thanks for the quick response.
So it will first sort “Quantity” column in descending order and then the “Total Sales” column right?
If so is it possible to make it otherwise without changing column order?

Hi @John.He,

Somehow I am not getting this method AddDataAreaCondition.
Is it version issue? If so then which version onwards it is there?

@Shivammirje1998,
Please try the latest version:
Aspose.Cells21.11.1 For .Net2_AuthenticodeSigned.Zip (5.6 MB)
Aspose.Cells21.11.1 For .Net4.0.Zip (5.6 MB)
Aspose.Cells21.11.1 For .NetStandard20.Zip (5.6 MB)

@Shivammirje1998,
First arrange the three numbers in Figure 1 in descending order, and then arrange the whole row of data in descending order.
Please check the attachment.sort result.PNG (84.5 KB)

Got it.
Thanks for all the help :slight_smile:

@Shivammirje1998,
Thanks for your feedback and using Aspose.Cells.
Let us know if you encounter any other issue, we will be glad to look into it and help you further.

If I use this then do I need take new license?

@Shivammirje1998,

Yes, you need to buy new license (if your license is already expired).

PS. you may open your license into notepad and check its subscription expiry date for confirmation. Please do not edit/update your license, least it will not work any more.

So for version update there’s no need to buy new license right (if old one is not expired)?

@Shivammirje1998,

Yes, your understanding is correct.

Hi @Amjad_Sahi and @John.He,

I want to set width of the row fields of the pivot table to a definite value. Could you please tell me how can I do it?

Best Regards,
Shivam

@Shivammirje1998,

Please note, I am able to reproduce an issue while setting columns’ width in pivot table. I found I could not extend columns widths if those columns are part of Pivot table. I tried to add the following lines to the code segment but to no avail:
e.g.
Sample code:

.........
CellArea area = pivotTable.RowRange; 
int start = area.StartColumn;
sheet.Cells.SetColumnWidthPixel(start, 220);

I have logged a ticket with an id “CELLSNET-49896” for your issue. We will look into it soon.

Once we have an update on it, we will let you know.

Thanks @Amjad_Sahi for your action. Please let me know how can I resolve the issue as soon as you get some updates on the ticket. :slight_smile:

@Shivammirje1998,

Sure, we will keep you posted with any updates (once available) on it.

@Shivammirje1998,

Currently, you can uncheck “AutoFit columns widths on update” to fix your issue for now. See the attached screenshot for your reference. And, we will provide a property to get and set “AutoFit columns widths on update” attribute programmatically. Please note, you have to set the property as false to make Cells.SetColumnWidth work.
image_2021_11_22T14_06_47_419Z.png (253.6 KB)

we will hope we can provide a fix within 1-2 days.

Hi @Amjad_Sahi,
Thanks for your response. I tried unchecking the “AutoFit columns widths on update” but as soon as new pivot table is created that option is already set to true. So I am not able to get required result.

I appreciate your efforts. :slight_smile:

And I am looking forward for the property to get and set this field programmatically.