gpt4 book ai didi

How to set up conditional formatting iconsets in Excel using C# NPOI? (C#, NPOI, Excel, MultistateFormatting, IconSet, IconSet Thresholds)(如何使用C#NPOI在Excel中设置条件格式图标集?(C#、NPOI、Excel、多态格式、图标集、图标集阈值))

转载 作者:bug小助手 更新时间:2023-10-25 14:01:32 34 4
gpt4 key购买 nike



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.

我想使用C# NPOI将条件交通灯图标集添加到Excel文件中,它会根据单元格中的值而变化。但不幸的是,没有关于此功能的好指南。


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.

由于我找不到任何关于如何在NPOI中设置带有条件格式的Excel图标集的有用帖子或指南,我想发布一个受此答案启发的解决方案。


You can format IconSets in NPOI by using this code (I'm using NPOI 2.5.6):

您可以使用以下代码(我使用的是NPOI 2.5.6)来格式化NPOI中的图标集:


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:

我发现在XML文件中为Excel工作表定义了正确的图标集,如下所示:



<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.

因为Threshold是一个字符串值,所以在将数值转换为字符串时应该小心。例如,将浮点数转换为字符串时,可以使用科学记数法或逗号。


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.

为了正确转换浮点数,您应该去掉科学记数法,而用点替换逗号,因为Excel不接受科学记数法和逗号。


This helped me overcome this problem.

这帮助我克服了这个问题。


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com