I wanted to add conditional traffic light iconsets to an Excel-file using C# NPOI, which would change depending on the value in a cell. But there are, unfortunately, no good guides on this functionality.
I tried several approaches, but nothing even remotely worked.
Since I could not find any helpful posts or guides as to how to set up an Excel icon set with conditional formatting in NPOI, I would like to post a solution inspired by this answer.
You can format IconSets in NPOI by using this code (I'm using NPOI 2.5.6):
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.OpenXmlFormats.Spreadsheet;
var cf = oSheet.GetCTWorksheet().AddNewConditionalFormatting(); //oSheet is an XSSFSheet.
cf.sqref = "C1"; //Or "C1:C2"
var rule = cf.AddNewCfRule();
rule.type = ST_CfType.iconSet;
rule.priority = 1;
var IconSet = rule.AddNewIconSet();
IconSet.iconSet = ST_IconSetType.Item3Signs; //You can choose another IconSet type here.
IconSet.showValue = true;
IconSet.reverse = true; //Reverses the default order of icons.
var value_1 = IconSet.AddNewCfvo();
value_1.type = ST_CfvoType.num; //Depending on what you need, you can choose precentages or other types. I found that using numbers when defining thresholds works better.
value_1.val = "0"; //Why you should start with a zero is further below.
var value_2 = IconSet.AddNewCfvo();
value_2.type = ST_CfvoType.num;
value_2.val = "10";
var value_3 = IconSet.AddNewCfvo();
value_3.type = ST_CfvoType.num;
value_3.val = "20";
Why would you need to add a zero to your thresholds?
I found out that a correct icon set is defined in the XML-file for the Excel sheet as follows:
<cfRule type="iconSet" priority="1">
<iconSet iconSet="3Signs" reverse="1">
<cfvo type="num" val="0" />
<cfvo type="num" val="10" />
<cfvo type="num" val="20" />
</iconSet>
</cfRule>
A note on the threshold values:
Since threshold is a string value you should be careful when casting numerical values as strings. For example, floating point numbers when cast to string can receive scientific notation or commas.
In order to correctly cast a floating point number you should get rid of scientific notation and replace comma with dot since Excel would not accept scientific notation and commas.
This helped me overcome this problem.