C# 如何给excel单元格的一半着色?

C# 如何给excel单元格的一半着色?,c#,excel,ms-office,office-interop,C#,Excel,Ms Office,Office Interop,背景:我需要把一个excele细胞涂成红色或绿色。如果单元格大于零,我需要将单元格涂成绿色(从单元格中间向右),如果单元格小于零,我需要将单元格涂成红色(从单元格中间向左) 我使用“Microsoft.Office.Interop.Excel”库 我该怎么做 p.S.不是重复的,因为我只想给excel单元格的一半上色,而不是全部上色。这(a)可能是作弊(b)作为评论(但没有图像)可能更好,(c)可能会扩大[excel]标记的意义,但可能有兴趣提及CF可以实现类似的功能: 列B(红色填充)的格式

背景:我需要把一个excele细胞涂成红色或绿色。如果单元格大于零,我需要将单元格涂成绿色(从单元格中间向右),如果单元格小于零,我需要将单元格涂成红色(从单元格中间向左)

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

我该怎么做

p.S.不是重复的,因为我只想给excel单元格的一半上色,而不是全部上色。

这(a)可能是作弊(b)作为评论(但没有图像)可能更好,(c)可能会扩大[excel]标记的意义,但可能有兴趣提及CF可以实现类似的功能:

列B(红色填充)的格式为公式规则:

=$B1<0  
=$B1>0  
作弊的部分是,B:C在宽度上被缩小,并且在整个选择中被格式化为中心

与火花线非常相似的东西:

@BrakNicku在一篇评论(带有图片链接)中指出,可以使用数据条(而图片恰恰证明,可以用颜色将Excel单元格填充一半)。变量(也是数据条)的长度与基础值成比例:

这(a)可能是欺骗(b)作为一个评论可能更好(但没有图像),和(c)可能会延伸[excel]标记的意义,但可能会有一些有趣的地方提到CF可以实现类似的功能:

列B(红色填充)的格式为公式规则:

=$B1<0  
=$B1>0  
作弊的部分是,B:C在宽度上被缩小,并且在整个选择中被格式化为中心

与火花线非常相似的东西:

@BrakNicku在一篇评论(带有图片链接)中指出,可以使用数据条(而图片恰恰证明,可以用颜色将Excel单元格填充一半)。变量(也是数据条)的长度与基础值成比例:


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

  • 在VBA中创建宏(使用鼠标)
  • 将宏重写为常规形式
  • 将宏保存在C#应用程序中
  • 用C#将宏保存在excel文件(xlsm)中
  • 从C#运行宏
  • 给定的宏:

    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
    

    如何保存宏并从C#

    运行宏为了解决这个问题,我使用了给定的方案:

  • 在VBA中创建宏(使用鼠标)
  • 将宏重写为常规形式
  • 将宏保存在C#应用程序中
  • 用C#将宏保存在excel文件(xlsm)中
  • 从C#运行宏
  • 给定的宏:

    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
    

    如何保存宏并从C#运行宏

    您可以通过vba执行此操作请参见此处或@Martheen的可能副本我只需要excel单元格的一半,而不是全部。您可以通过vba执行此操作请参见此处或@Martheen的可能副本我只需要excel单元格的一半,没有满。你能说如何使用C#吗?也可以用CF数据条(最小值和最大值都设置为0)模拟这种格式@h.o.m.a.n你可以用C#设置数据库吗?也可以用CF数据条(最小值和最大值都设置为0)模拟这种格式@h.o.m.a.n您可以根据您的帖子(您提供了wondefurl图片)和BrakNicku的评论设置带有@pnuts的数据库,我决定使用vba宏为excel单元格的一半着色,并通过C#运行它。@pnuts根据您的帖子(您提供了wondefurl图片)和BrakNicku的评论,我决定使用vba宏为excel单元格的一半着色,并通过C#运行它。