Search code examples
c#excelms-officeoffice-interop

How to color a half of an excel cell?


Background: I need to color an excele cell in red or green color. If cell is more zero that I need to color the cell in green (in right way from the middle of the cell), if the cell is less zero I need to color the cell in red (in left way from the middle of the cell).

I use "Microsoft.Office.Interop.Excel" library.

How shall I do this?

P.S. Cell color changing In Excel using C# is not a duplicate, because of i want to color only half of an excel cell, not full.


Solution

  • This (a) may be cheating (b) perhaps better as a Comment (but then there would be no image) and (c) possibly stretching the significance of the [excel] tag here, but may be of some interest to mention that CF can achieve something of the sort:

    SO39243927 first example

    ColumnB (red Fill) being formatted with a formula rule of:

    =$B1<0  
    

    and ColumnC (green Fill) with a formula rule of:

    =$B1>0  
    

    The cheating part is that B:C have been reduced in width and formatted Center Across Selection.

    Something very vaguely similar with Sparklines:

    SO39243927 second example

    In a Comment (with link to an image) @BrakNicku has pointed out that Data Bars could be applied (and the image rather proves that it is possible to half fill an Excel cell with colour). A variation, also Data Bars, is to have the length proportional to the underlying value:

    SO39243927 third example