question

GaniTPT avatar image
0 Votes"
GaniTPT asked Viorel-1 answered

How to make it change excel cell font color as black in template using c#

I am using below source code to highlight the font color as red if any difference in the value.

Now, before highlighting the color, i just want to set font color in the text as "black" in the defined range template.

How to do this..?

other reference post : how-do-we-highlight-the-cells-in-same-excel-templa.html


 public static void WriteToFile(object[,] data, List<Tuple<int, int>> cellsToChangeColor,string rangeStart,string rangeEnd) 
          {
              Range range = xlWorksheet.get_Range(rangeStart, rangeEnd);
       //set text font color as "black" in entire range template, before highlighting the different color
    
              range.set_Value(XlRangeValueDataType.xlRangeValueDefault, data);
              xlWorksheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange, range,
             Type.Missing, XlYesNoGuess.xlYes, Type.Missing).Name = "TestTable";
              xlWorksheet.ListObjects["TestTable"].TableStyle = "TableStyleMedium2";
        
              List<char> columnToExclude = new List<char>() { 'D', 'H', 'I' };
              foreach (var item in cellsToChangeColor)
              {
                  char column = (char)(item.Item2 + 64);
                  if (columnToExclude.Contains(column)) continue;
                  string cell = (char)(item.Item2 + 64) + item.Item1.ToString();
                  range.Range[cell].Font.Color = ColorTranslator.ToOle(Color.Red);
              }
          }


dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Hi @GaniTPT,
>>i just want to set font color in the text as "black" in the defined range template.
Do you want to set all the cells or the highlighted part? Please explain in detail.
And please refer to the following code:

 // Access the range of cells to be formatted.
 Range range = worksheet.Range["A3:E6"];
 // Begin updating of the range formatting. 
 Formatting rangeFormatting = range.BeginUpdateFormatting();
 // Specify font settings (font name, color).
 rangeFormatting.Font.Name = "test";
 rangeFormatting.Font.Color = Color.Black;

Best Regards,
Daniel Zhang

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Did you try 'range.Font.Color = ColorTranslator.ToOle(Color.Black)'?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks and It's working...

Tested the entire sheet for changing the font color before highlighting.

'range.Font.Color = ColorTranslator.ToOle(Color.Black)'

0 Votes 0 ·