excelcolorsasposeaspose-cellsbackground-foreground

Why does setting a color to a cell not work (Aspose Cells)?


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:

enter image description here

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 the screenshot above.


Solution

  • Please change your code segment to (see the highlighted lines): 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.

    I am working as Support developer/ Evangelist at Aspose.