We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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

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;

As you can see, 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.

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

Hi,


Thanks for providing us details and code segments.

I have evaluated your issue/case a bit. I think you need to make the relevant StyleFlag options on to apply proper formatting to the cell(s). See the updated code segment for your reference:
e.g
Sample code:

CellsFactory cfDate = new CellsFactory();
Cell dateCell = pricePushSheet.Cells[3, 4];
Style styleDate = cfDate.CreateStyle();
dateCell.PutValue(pricePushSheet.Cells[7, 1]);
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”;

StyleFlag flag2 = new StyleFlag();
flag2.NumberFormat = true;
flag2.CellShading = true;
flag2.HorizontalAlignment = true;
flag2.FontColor = true;
flag2.FontBold = true;

dateCell.SetStyle(styleDate, flag2);

Hope, this helps a bit.

Thank you.