Excel条件设置

本文关键字:设置 条件 Excel | 更新日期: 2023-09-27 18:04:54

我试图设置一些条件设置值,但似乎他们没有采取。当我查看excel文档时,它们是默认的。
绿色= 0.2
黄色= 0.1

显示为
绿色0.8
黄色的

0.6
private static void setIcon(Excel.Worksheet excelWorksheet, string cell)
{
    try
    {
        Excel.IconSetCondition cfIconSet = (Excel.IconSetCondition)excelWorksheet.Range[cell, cell].FormatConditions.AddIconSetCondition();
        cfIconSet.IconCriteria[1].Icon = Excel.XlIcon.xlIconRedTrafficLight;
        cfIconSet.IconCriteria[2].Type = Excel.XlConditionValueTypes.xlConditionValueNumber;
        cfIconSet.IconCriteria[2].Icon = Excel.XlIcon.xlIconYellowTrafficLight;
        cfIconSet.IconCriteria[2].Value = Convert.ToDouble(yellow);
        cfIconSet.IconCriteria[2].Operator = (int)(Excel.XlFormatConditionOperator.xlGreaterEqual);
        cfIconSet.IconCriteria[3].Type = Excel.XlConditionValueTypes.xlConditionValueNumber;
        cfIconSet.IconCriteria[3].Value = Convert.ToDouble(green);
        cfIconSet.IconCriteria[3].Icon = Excel.XlIcon.xlIconGreenTrafficLight;
        cfIconSet.IconCriteria[3].Operator = (int)(Excel.XlFormatConditionOperator.xlGreaterEqual);
    }
    catch (Exception ex)
    {
        throw new ArgumentException(ex.Message);
    }
}

Excel条件设置

我使用了下面的代码。

var c = (Excel.IconSetCondition)excelWorksheet.get_Range(cell).FormatConditions.AddIconSetCondition();
c.SetFirstPriority();
c.ShowIconOnly = false;
c.IconSet = book.IconSets[Excel.XlIconSet.xl3TrafficLights2];
var yellowIcon = c.IconCriteria[2];
yellowIcon.Type = Excel.XlConditionValueTypes.xlConditionValueNumber;
yellowIcon.Value = Convert.ToDouble(yellow);
yellowIcon.Operator = (int)Excel.XlFormatConditionOperator.xlGreaterEqual;
var greenIcon = c.IconCriteria[3];
greenIcon.Type = Excel.XlConditionValueTypes.xlConditionValueNumber;
greenIcon.Value = Convert.ToDouble(green);
greenIcon.Operator = (int)Excel.XlFormatConditionOperator.xlGreaterEqual;