条件格式与图标Epplus

时间:2015-02-05 10:33:42

标签: epplus epplus-4

我需要用Epplus来实现这样的目标。

enter image description here

有人可以使用我需要使用的代码来指导我。

1 个答案:

答案 0 :(得分:3)

以下是完全符合您要求的代码,但是对于三个图标集,您可以根据您的图标进行更改。如果值大于4,我将设置红色箭头,如果值介于1和4之间,则设置为黄色箭头,如果小于1,则设置为绿色。只需将“AddThreeIconSet”更改为图标即可。你应该明白这个想法。

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;
       }
    }
}