Please refer to the following code below for your reference. You may use PivotTable.PivotFormatConditions attribute to add formatted conditions for conditional formattings on your pivot table report for your needs.
Workbook workbook = new Workbook();
//Create data source Table1
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells[0, 0].Value = “Header1”;
worksheet.Cells[0, 1].Value = “Header2”;
worksheet.Cells[0, 2].Value = “Header3”;
for (int i = 1; i < 21; i++)
{
int value = i - 1;
worksheet.Cells[i, 0].Value = “Value1-” + (value < 10 ? “a” : “b”);
worksheet.Cells[i, 1].Value = “Value2-” + value;
worksheet.Cells[i, 2].Value = value;
}
worksheet.ListObjects.Add(0, 0, 20, 2, true);
//Create PivotTable
workbook.Worksheets.Add(“PivotSheet”);
worksheet = workbook.Worksheets[1];
int index = worksheet.PivotTables.Add("=Sheet1!A1:C21", “A1”, “PT”);
PivotTable pivotTable = worksheet.PivotTables[index];
pivotTable.AddFieldToArea(PivotFieldType.Row, “Header1”);
pivotTable.AddFieldToArea(PivotFieldType.Row, “Header2”);
pivotTable.AddFieldToArea(PivotFieldType.Data, “Header3”);
pivotTable.RefreshData();
pivotTable.CalculateData();
var fc = pivotTable.PivotFormatConditions[pivotTable.PivotFormatConditions.Add()];
fc.ScopeType = PivotConditionFormatScopeType.field;
FormatConditionCollection fcs = fc.FormatConditions;
CellArea area = new CellArea
{
StartRow = 1,
EndRow = 23,
StartColumn = 2,
EndColumn = 2
};
FormatCondition condition = fcs[fcs.Add(area, FormatConditionType.DataBar, OperatorType.Equal, “”, “”)[0]];
condition.Type = FormatConditionType.DataBar;
fcs.AddArea(area);
condition.DataBar.MinCfvo.Type = FormatConditionValueType.Number;
condition.DataBar.MinCfvo.Value = 0;
condition.DataBar.MaxCfvo.Type = FormatConditionValueType.Number;
condition.DataBar.MaxCfvo.Value = 1;
condition.DataBar.Color = Color.Green;
condition.DataBar.ShowValue = true;
workbook.Save(“e:\test2\condi_pivottable.xlsx”);