Hello,
After upgrading from 23.8.0 to 24.12.0, it looks like the code for pivot table conditional formatting stopped working - the rules are just not added in Excel.
Aspose.Cells version: 24.12.0
C# target framework: .NET 8
Code used to generate the xlsx file:
using Aspose.Cells;
using Aspose.Cells.Pivot;
using System.Drawing;
using System.Text;
byte[] SrcDataByteArray = Encoding.ASCII.GetBytes(
$@"City,Product,Sales
Paris,Cream,2300
Paris,Lotion,1600
Tunis,Cream,900
Tunis,Lotion,1400
Tunis,Cream,3090
Tunis,Lotion,6000
Paris,Cream,4320");
// Create a memory stream from the source data
MemoryStream dataStream = new MemoryStream(SrcDataByteArray);
// Create LoadOptions class object to load the comma-separated data given above
LoadOptions loadOptions = new LoadOptions(LoadFormat.Csv);
// Instantiate a workbook class object having above mentioned data
Workbook wbCSV = new Workbook(dataStream, loadOptions);
// Get access to the first worksheet in the collection
Worksheet targetSheet = wbCSV.Worksheets[0];
// Get collection of pivot tables in the target worksheet
Aspose.Cells.Pivot.PivotTableCollection pvTablesCollection = targetSheet.PivotTables;
// Get pivot table index after adding a new pivot table by provding source data range and destination cell
int iNewPivotTable = pvTablesCollection.Add("=A1:C8", "F3", "MyPivotTable");
// Get the instance of newly added pivot table for further processing
Aspose.Cells.Pivot.PivotTable excelPivot = pvTablesCollection[iNewPivotTable];
// Hide the grand total for rows in the output Excel file
excelPivot.RowGrand = false;
excelPivot.ColumnGrand = false;
// Add the first field to the column area
excelPivot.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 0);
// Add the second field to the row area
excelPivot.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 1);
// Add the third field to the data area
excelPivot.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
// Conditional formatting
//int ind = targetSheet.ConditionalFormattings.Add();
//FormatConditionCollection fcs = targetSheet.ConditionalFormattings[ind];
//CellArea cellArea = new CellArea()
//{
// StartRow = 1,
// EndRow = 7,
// StartColumn = 2,
// EndColumn = 2
//};
//fcs.AddArea(cellArea);
//int indexCond = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.GreaterThan, "1", null);
//fcs[indexCond].Style.BackgroundColor = ColorTranslator.FromHtml("#e39e9e");
int ind = excelPivot.PivotFormatConditions.Add();
PivotFormatCondition pivForCond = excelPivot.PivotFormatConditions[ind];
pivForCond.ScopeType = PivotConditionFormatScopeType.Data;
pivForCond.AddDataAreaCondition(excelPivot.DataFields[0]);
pivForCond.SetConditionalAreas();
int indexCond = pivForCond.FormatConditions.AddCondition(FormatConditionType.CellValue, OperatorType.GreaterThan, "1", null);
pivForCond.FormatConditions[indexCond].Style.BackgroundColor = ColorTranslator.FromHtml("#e39e9e");
// Saving the output Excel file with pivot table
wbCSV.Save("OutputPivotTable.xlsx");
System.Console.WriteLine("Done");
Note also the commented out section which works on regular cells, but not on the pivot table.
The examples are taken from (I couldn’t find a pivot table cond format example)
Thank you