Thanks for the template file.
Well, you may try to use PivotTable.RowRange, PivotTable.DataBodyRange to get the row area and data area in the PivotTable report and apply formatting to the specified cells range accordingly. There are PivotTable.Format and PivotTable.FormatAll() methods that you may use too. See the following sample code for your reference, I have used your template file to update the formatting of some areas in the PivotTable:
var workbook = new Workbook(“e:\test2\PivotTableSample.xlsx”);
var pivot = workbook.Worksheets[0].PivotTables[0];
// pivot.RefreshData();
// pivot.CalculateData();
// pivot.CalculateRange();
Style style1 = workbook.CreateStyle();
style1.ForegroundColor = Color.LightGray;
style1.Pattern = BackgroundType.Solid;
Style style2 = workbook.CreateStyle();
style2.ForegroundColor = Color.Gray;
style2.Pattern = BackgroundType.Solid;
Style style3 = workbook.CreateStyle();
style3.ForegroundColor = Color.LightGreen;
style3.Pattern = BackgroundType.Solid;
Style style4 = workbook.CreateStyle();
style4.ForegroundColor = Color.GreenYellow;
style4.Pattern = BackgroundType.Solid;
CellArea area = pivot.RowRange; //A5:A8
CellArea area2 = pivot.DataBodyRange;//B5:C8
int start = area.StartColumn;
for (int i = area.StartRow; i <= area.EndRow; i++)
{
for (int j = area.StartColumn; j <= area.EndColumn; j++)
{
if (i % 2 == 0)
{
pivot.Format(i, j, style3);
}
else
{
pivot.Format(i, j, style4);
}
}
}
for (int r = area2.StartRow; r <= area2.EndRow; r++)
{
for (int c = area2.StartColumn; c <= area2.EndColumn; c++)
{
if (r % 2 == 0)
{
pivot.Format(r, c, style1);
}
else
{
pivot.Format(r, c, style2);
}
}
}
workbook.Save(“e:\test2\out1.xlsx”);
Hope, this helps a bit.
Thank you.