I have this code to try to set the background color of a cell (among other things):
private static readonly Color CONTRACT_ITEM_COLOR = Color.FromArgb(255, 255, 204);
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.BackgroundColor = CONTRACT_ITEM_COLOR;
pivotTableSheet.Cells[4, 0].SetStyle(style);
The setting of horizontal and vertical alignment works, as does bold and height - everything but color, as can be seen in the screenshot below.
What is yet needed? I have even tried setting ForegroundColor as well as Background colors, to :
style.ForegroundColor = Color.Red;
style.BackgroundColor = Color.Blue;
...but neither does anything - the cell still looks exactly the same as before.
Hi,
Thanks for providing us sample code with details.
Please change your code segment to (see the lines in bold):
e.g
Sample code:
. . .
cell = pivotTableSheet.Cells[4, 0];
cell.PutValue(AnnualContractProductsLabel);
style = cell.GetStyle();
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
style.Font.IsBold = true;
pivotTableSheet.Cells.SetRowHeight(4, 25);
style.ForegroundColor = CONTRACT_ITEM_COLOR;
style.Pattern = BackgroundType.Solid;
pivotTableSheet.Cells[4, 0].SetStyle(style);
…
it should work fine.
Let us know if you still have any issue.
Thank you.
Thanks! That works!
It still doesn’t work on ranges in a Pivot Table, though (only on “regular old” cells).
Which is:
Range rangeToColorize = pivotTableSheet.Cells.CreateRange(
currentRowBeingExamined, 0,
ROWS_BETWEEN_DESCRIPTIONS, _grandTotalsColumnPivotTable + 2);
Style style;
style = workBook.CreateStyle();
style.ForegroundColor = CONTRACT_ITEM_COLOR; // Color.Red;
style.Pattern = BackgroundType.Solid;
StyleFlag styleFlag = new StyleFlag();
styleFlag.Font = true;
styleFlag.CellShading = true;
rangeToColorize.ApplyStyle(style, styleFlag);