excelasposeaspose-cellsbackground-foreground

Why is my font and background (foreground) colorization not working here (Aspose Cells)?


In two places on my sheet, I need cells that have white font and a black background. In one place (the header row), it works; in the other (a date value), it doesn't, and I don't see what I'm doing differently that would cause this failure.

Here is the code that is working (for the header row):

CellsFactory cfHeaderRow = new CellsFactory();
Cell headerRowCell;
Style styleHeaderRow;
for (int x = 0; x < drPrices.FieldCount-1; x++)
{
    headerRowCell = pricePushSheet.Cells[6, x]; 
    headerRowCell.PutValue(drPrices.GetName(x));
    pricePushSheet.Cells.SetColumnWidth(x, 9); 
    styleHeaderRow = cfHeaderRow.CreateStyle();
    styleHeaderRow.HorizontalAlignment = TextAlignmentType.Center;
    styleHeaderRow.Font.Color = Color.White;
    styleHeaderRow.ForegroundColor = Color.Black;
    styleHeaderRow.Pattern = BackgroundType.Solid;
    styleHeaderRow.IsTextWrapped = true;
    headerRowCell.SetStyle(styleHeaderRow);
}

..and here is the code that is not working (for the date row circled in the screen shot below):

CellsFactory cfDate = new CellsFactory();
Cell dateCell = pricePushSheet.Cells[3, 4];
Style styleDate = cfDate.CreateStyle();
dateCell.PutValue(pricePushSheet.Cells[7, 1]);
StyleFlag flag2 = new StyleFlag();
flag2.NumberFormat = true;
styleDate.Font.IsBold = true;
styleDate.HorizontalAlignment = TextAlignmentType.Center;
styleDate.Font.Color = Color.White;
styleDate.ForegroundColor = Color.Black;
styleDate.Pattern = BackgroundType.Solid;
styleDate.Custom = "mm/dd/yyyy";
dateCell.SetStyle(styleDate, flag2);

So what seems to be different (that might matter) in the non-working code is that it uses a StyleFlag (because it needs to set the date format):

StyleFlag flag2 = new StyleFlag();
flag2.NumberFormat = true;
styleDate.Custom = "mm/dd/yyyy";

...and, of course, the SetStyle takes the flag as a second arg (the working bit of code needs no flag, because it's not doing anything fancy).

As you can see below, there isn't a date in the cell in question (E4); it grabs it from a place that isn't on the sheet yet. But the fact that the font is black instead of white, and the background (called foreground by Aspose) is white instead of black.

enter image description here

So why is the coloring not working? What do I need to do or change to get it to work in cell E4?


Solution

  • Please note, as you wish to apply the cell shading as well as the font colors to the cell in question therefore you need to set the appropriate StyleFlag properties to true so that the aforementioned style aspects could take effect. Please check the following piece of code that is producing expected results.

    var workbook = new Workbook(dir + "book1.xlsx");
    var pricePushSheet = workbook.Worksheets[0];
    var cfDate = new CellsFactory();
    var dateCell = pricePushSheet.Cells[3, 4];
    var styleDate = cfDate.CreateStyle();
    dateCell.PutValue(pricePushSheet.Cells[7, 1].Value);
    var flag2 = new StyleFlag();
    flag2.NumberFormat = true;
    flag2.CellShading = true;
    flag2.Font = true;
    styleDate.Font.IsBold = true;
    styleDate.HorizontalAlignment = TextAlignmentType.Center;
    styleDate.Font.Color = Color.White;
    styleDate.ForegroundColor = Color.Black;
    styleDate.Pattern = Aspose.Cells.BackgroundType.Solid;
    styleDate.Custom = "mm/dd/yyyy";
    dateCell.SetStyle(styleDate, flag2);
    

    enter image description here

    Note: I am working as Developer Evangelist at Aspose.