Font color

Hello,

I wanted to make the font color of cells which contains #NA values the same like the background color of the same cell, so that it will look like empty cell. I use the following code:

cells[1,1].Style.Font.Color = cells[1, 1].Style.BackgroundColor;

but it doesn’t work. If I use this:

cells[1,1].Style.Font.Color = System.Drowing.Color.White;

it works. But the backgroung of the cells can be blue or red. So I can not use this code. What can I do?

Thanks. Boris.

Hi Boris,

Please try this:

cells[1,1].Style.Font.Color = cells[1, 1].Style.ForegroundColor;

Hi Laurence,

it works but only if the background is something other than default (white). In this case I see black letters on white background.

Can you please explain me what is the difference between BackgroundColor and ForegroundColor. In which cases I have to use which property.

Thanks.

Boris

Hi Boris,

You can try this:

if(cells[1,1].Style.ForegroundColor == Color.Empty)
cells[1,1].Style.Font.Color = Color.White;
else
cells[1,1].Style.Font.Color = cells[1, 1].Style.ForegroundColor;


If you format a cell in MS Excel, you can choose different “Pattern” in “Patterns” setting. In default state, the color your set is foreground color. Only you change the Pattern, you can set the background color.

Thank you. It works Smile

Boris

@Borjanja,
Aspose.Excel is deprecated now and a new product Aspose.Cells has replaced it which supports all the features of Aspose.Excel along with the support for the latest features in different versions of MS Excel. We can work with fonts and its parameters in a variety of ways. Following is an example which demonstrates changing the font color of a cell using Aspose.Cells:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
    System.IO.Directory.CreateDirectory(dataDir);

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];

// Accessing the "A1" cell from the worksheet
Aspose.Cells.Cell cell = worksheet.Cells["A1"];

// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");

// Obtaining the style of the cell
Style style = cell.GetStyle();

// Setting the font to be underlined
style.Font.Underline = FontUnderlineType.Single;

//Setting the color
style.Font.Color = Color.Blue;

// Applying the style to the cell
cell.SetStyle(style);

// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

Here is a detailed article which can be referred while dealing with fonts:
Dealing with Font Settings

You may try the latest free trail version of this product here:
Aspose.Cells for .NET (Latest Version)

You can test different features of this new product by downloading an executable solution here.