6

I am using Microsoft.Office.Interop.Excel library.

I have a cell with values "Green Red". What I want is pretty simple. I want to insert "Green" text to be green and "Red" to be red, like that:

enter image description here

I am using this code to insert data in cell:

Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();
// single worksheet
Excel._Worksheet workSheet = excelApp.ActiveSheet;

for (int startIndex = 0; startIndex < 10; startIndex++)
{
    workSheet.Cells[1, (startIndex + 1)] ="Green" + " Red";
}

How to do it?

I've tried this approach, but I do not know what [RangeObject] is:

[RangeObject].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

Community
  • 1
  • 1
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • Does http://stackoverflow.com/questions/5667842/multiple-formats-in-one-cell-using-c-sharp help you - which way of the Excel integration from C# are you using? – weismat Apr 07 '16 at 11:54
  • @weismat ooops, I cannot understand your question.I am using `Microsoft.Office.Interop.Excel` library. – StepUp Apr 07 '16 at 11:58
  • Look at: [http://stackoverflow.com/questions/2452417/cell-color-changing-in-excel-using-c-sharp](http://stackoverflow.com/questions/2452417/cell-color-changing-in-excel-using-c-sharp) – Adam Calvet Bohl Apr 07 '16 at 12:03
  • @AdamCalvetBohl yeah, I've tried, but cannot figure out what `[RangeObject]` is. – StepUp Apr 07 '16 at 12:04
  • @StepUp I'm not familiar with your language, but In Excel VBA, one would use the `Characters` property of the `Range` object to change the font of individual characters within a text string in a cell. Does that help? – Ron Rosenfeld Apr 07 '16 at 12:12
  • 1
    A range object `Represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range.` For your case applying two colors to one cell it would not be useful. – Brian from state farm Apr 07 '16 at 12:41

1 Answers1

19

Try:

workSheet.Cells[1, (i + 1)].Characters[start_pos, len].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

where start_pos and len is the part of the string where to apply color.

Your use case example:

    Application excelApp = new Application();
    excelApp.Workbooks.Add();
    // single worksheet
    _Worksheet workSheet = excelApp.ActiveSheet;

    string Green = "Green";
    string Red = "Red";
    for (int start = 0; start < 10; start++)
    {
        Range ColorMeMine = workSheet.Cells[1, (start + 1)];
        ColorMeMine.Value = string.Format("{0} {1}", Green, Red);
        ColorMeMine.Characters[0, Green.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
        ColorMeMine.Characters[Green.Length + 1, Green.Length + 1 + Red.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
    }
Brian from state farm
  • 2,825
  • 12
  • 17
Adam Calvet Bohl
  • 1,009
  • 14
  • 29
  • what is `Range`? What's a library should I use to get `Range` class? – StepUp Apr 07 '16 at 13:35
  • 3
    A Range "Represents a cell, a row, a column, a selection of cells containing one or more contiguous blocks of cells, or a 3-D range". You find it in Microsoft.Office.Interop.Excel namespace. – Adam Calvet Bohl Apr 07 '16 at 13:39
  • Cool, this is working just fine. Just remember to add System.Drawing as a reference. – AH. May 05 '20 at 04:54