如何上色一个Excel单元格的一半?

背景:我需要为红色或绿色的excele单元格着色。 如果单元格更加零,我需要用绿色(从单元格中间的正确方式)给单元格着色,如果单元格不是零,我需要用红色给单元格着色(从单元格的中间向左)。

我使用“Microsoft.Office.Interop.Excel”库。

我该怎么做?

PS 单元格的颜色变化在Excel中使用C#不是重复的,因为我想只上色一半的Excel单元格,而不是全部。

(a)可能作为一个评论作弊(但是不会有图像),或者(c)可能在这里扩展[excel]标签的意义,但可能有一些兴趣提到CF可以实现这样的事情:

SO39243927第一个例子

ColumnB(红色填充)被格式化,公式规则为:

=$B1<0 

和ColumnC(绿色填充),公式规则为:

 =$B1>0 

作弊的部分是,B:C的宽度已经减小,并且格式化了“中心select”。

与Sparklines 非常类似的东西:

SO39243927第二个例子

在一个评论(与链接到图像)@BrakNicku指出,数据条可以应用(和图像,而不是certificate有可能是一半的Excel单元格填充颜色)。 一个变化,也是数据吧,是有长度成正比的基础价值:

SO39243927第三个例子

为了解决这个问题,我使用了给定的scheme:

  1. 在VBA中创buildmacros(通过使用鼠标)。
  2. 将macros重写为一般forms。
  3. 将macros保存在C#应用程序中。
  4. 通过C#将macros保存在excel文件(xlsm)中。
  5. 从C#运行macros。

给定的macros:

 Sub CreateGistograms(r As String) Range(r).Select Selection.FormatConditions.AddDatabar Selection.FormatConditions(Selection.FormatConditions.Count).ShowValue = True Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1) .MinPoint.Modify newtype:=xlConditionValueAutomaticMin .MaxPoint.Modify newtype:=xlConditionValueAutomaticMax End With With Selection.FormatConditions(1).BarColor .Color = 8700771 .TintAndShade = 0 End With Selection.FormatConditions(1).BarFillType = xlDataBarFillGradient Selection.FormatConditions(1).Direction = xlContext Selection.FormatConditions(1).NegativeBarFormat.ColorType = xlDataBarColor Selection.FormatConditions(1).BarBorder.Type = xlDataBarBorderSolid Selection.FormatConditions(1).NegativeBarFormat.BorderColorType = _ xlDataBarColor With Selection.FormatConditions(1).BarBorder.Color .Color = 8700771 .TintAndShade = 0 End With Selection.FormatConditions(1).AxisPosition = xlDataBarAxisAutomatic With Selection.FormatConditions(1).AxisColor .Color = 0 .TintAndShade = 0 End With With Selection.FormatConditions(1).NegativeBarFormat.Color .Color = 255 .TintAndShade = 0 End With With Selection.FormatConditions(1).NegativeBarFormat.BorderColor .Color = 255 .TintAndShade = 0 End With End Sub 

如何保存macros并运行他从C# 在运行时在dotnet创buildmacros