Epplus基于规则的条件格式

时间:2015-02-09 02:49:43

标签: c# epplus

我们如何使用epplus条件格式为excel中的每个单元格添加三个图标集。我正在使用以下代码添加三个图标集:

using (ExcelRange scoreRange = workSheet.Cells[1, 2, 1, 10])
            {
                ExcelAddress rangeAddress = new ExcelAddress(scoreRange.Address);
                var ruleIconSet = workSheet.ConditionalFormatting.AddThreeIconSet(rangeAddress, eExcelconditionalFormatting3IconsSetType.Arrows); // This calculates based on the value in the range
            }

我想创建一个规则,如果单元格中的值小于0,则应显示绿色图标,如果值大于0,则应显示红色图标。

可以执行此操作的规则声明应该是什么?

1 个答案:

答案 0 :(得分:0)

for(int j =2; j <=9; j++) //Loop through columns
{
   for(int i = 3; i <= 12; i++) // Loop through rows
   {
   // gets only the current cell as range
   ExcelRange rng = worksheet.Cells[i, j, i, j]; 
   ExcelAddress address = new ExcelAddress(rng.Address);
   // Get the value of the current cell
   if(Convert.ToDouble(worksheet.Cells[i, j].Value) >= 4.0)
   {
      var v = worksheet.ConditionalFormatting.AddThreeIconSet(address, eExcelconditionalFormatting3IconSetType.Arrows);
      v.Reverse = true;
      v.Icon1.Type = eExcelConditionalFormattingValueObjectType.Num;
   }
   else if(Convert.ToDouble(workSheet.Cells[i, j].Value) > 1.0 && Convert.ToDouble(workSheet.Cells[i, j].Value) < 4.0)
   {

      var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
      v.Icon3.Type = eExcelConditionalFormattingValueObjectType.Num;

   }
   else (Convert.ToDouble(workSheet.Cells[i, j].Value)  < 1.0)
   {
      var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
      v.Icon2.Type = eExcelConditionalFormattingValueObjectType.Num;
   }
}
}

这适合我。