-1

我正在尝试设置一些条件设置值,但似乎它们没有采用。当我查看 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);
    }
}
4

1 回答 1

1

我使用了以下有效的方法。

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;
于 2013-03-20T04:14:28.283 回答