2

I need to achieve something like this with Epplus.

enter image description here

Can someone guide me with the code I need to use.

MikeFerrer
  • 77
  • 1
  • 10

1 Answers1

4

Following is the code to do exactly what you want but it is for three icon set you can change it based on your icons. I'm setting red color arrow if value is greater than 4,yellow color arrow if value is between 1 and 4 and, finally, green color if it is less than 1. Just change "AddThreeIconSet" to your icons. You should get the idea with this.

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, eExcelconditionalFormatting3IconsSetType.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 if (Convert.ToDouble(workSheet.Cells[i, j].Value) < 1.0)
       {
          var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
          v.Icon2.Type = eExcelConditionalFormattingValueObjectType.Num;
       }
    }
}
Community
  • 1
  • 1
sanmis
  • 515
  • 1
  • 7
  • 22
  • You can also use this http://stackoverflow.com/questions/28493050/importing-excel-file-with-all-the-conditional-formatting-rules-to-epplus It allows you to have any formatting first done in excel, import it as xml and then use it. This way, you can have any kind of conditional formatting along with any type of rules. – sanmis Feb 16 '15 at 03:02
  • I am unsure where you actually set the colors? – chri3g91 Nov 16 '18 at 13:09