Skip to content

Commit 663b41a

Browse files
authored
Merge pull request #174 from SyncfusionExamples/261512-TrafficLightFaqExample
261512-How to set traffic lights icon in Excel conditional formatting using XlsIO?
2 parents 01637b6 + 8ee34dc commit 663b41a

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# How to set traffic lights icon in Excel conditional formatting using C#?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.XlsIO;
15+
```
16+
17+
Step 5: Include the below code snippet in **Program.cs** to set traffic lights icon in Excel conditional formatting using C#
18+
```csharp
19+
using (ExcelEngine excelEngine = new ExcelEngine())
20+
{
21+
IApplication application = excelEngine.Excel;
22+
application.DefaultVersion = ExcelVersion.Excel2013;
23+
IWorkbook workbook = application.Workbooks.Create(1);
24+
IWorksheet worksheet = workbook.Worksheets[0];
25+
26+
//Add data and formatting to the worksheet
27+
worksheet.Range["A1"].Text = "Traffic Lights";
28+
29+
//Add percentage values to cells A2 to A7 and format them as percentages
30+
worksheet.Range["A2"].Number = 0.95;
31+
worksheet.Range["A2"].NumberFormat = "0%";
32+
worksheet.Range["A3"].Number = 0.5;
33+
worksheet.Range["A3"].NumberFormat = "0%";
34+
worksheet.Range["A4"].Number = 0.1;
35+
worksheet.Range["A4"].NumberFormat = "0%";
36+
worksheet.Range["A5"].Number = 0.9;
37+
worksheet.Range["A5"].NumberFormat = "0%";
38+
worksheet.Range["A6"].Number = 0.7;
39+
worksheet.Range["A6"].NumberFormat = "0%";
40+
worksheet.Range["A7"].Number = 0.6;
41+
worksheet.Range["A7"].NumberFormat = "0%";
42+
43+
//Adjust row height and column width of the used range
44+
worksheet.UsedRange.RowHeight = 20;
45+
worksheet.UsedRange.ColumnWidth = 25;
46+
47+
//Apply the first conditional format
48+
IConditionalFormats condition = worksheet.UsedRange.ConditionalFormats;
49+
IConditionalFormat condition1 = condition.AddCondition();
50+
51+
condition1.FormatType = ExcelCFType.CellValue;
52+
condition1.FirstFormula = "300";
53+
condition1.Operator = ExcelComparisonOperator.Less;
54+
condition1.FontColor = ExcelKnownColors.Black;
55+
condition1.BackColor = ExcelKnownColors.Sky_blue;
56+
57+
//Apply the second conditional format
58+
IConditionalFormats condition2 = worksheet.UsedRange.ConditionalFormats;
59+
IConditionalFormat condition3 = condition2.AddCondition();
60+
condition3.FormatType = ExcelCFType.IconSet;
61+
IIconSet iconSet = condition3.IconSet;
62+
iconSet.IconSet = ExcelIconSetType.ThreeTrafficLights1;
63+
64+
//Saving the workbook
65+
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write);
66+
workbook.SaveAs(outputStream);
67+
outputStream.Dispose();
68+
}
69+
```
70+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Traffic lights icon", "Traffic lights icon\Traffic lights icon.csproj", "{E5911C99-3E19-4A51-906E-6E97D116A4AE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{E5911C99-3E19-4A51-906E-6E97D116A4AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E5911C99-3E19-4A51-906E-6E97D116A4AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E5911C99-3E19-4A51-906E-6E97D116A4AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E5911C99-3E19-4A51-906E-6E97D116A4AE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Output/.gitkeep

Whitespace-only changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.XlsIO;
4+
5+
namespace Traffic_Light
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (ExcelEngine excelEngine = new ExcelEngine())
12+
{
13+
IApplication application = excelEngine.Excel;
14+
application.DefaultVersion = ExcelVersion.Excel2013;
15+
IWorkbook workbook = application.Workbooks.Create(1);
16+
IWorksheet worksheet = workbook.Worksheets[0];
17+
18+
//Add data and formatting to the worksheet
19+
worksheet.Range["A1"].Text = "Traffic Lights";
20+
21+
//Add percentage values to cells A2 to A7 and format them as percentages
22+
worksheet.Range["A2"].Number = 0.95;
23+
worksheet.Range["A2"].NumberFormat = "0%";
24+
worksheet.Range["A3"].Number = 0.5;
25+
worksheet.Range["A3"].NumberFormat = "0%";
26+
worksheet.Range["A4"].Number = 0.1;
27+
worksheet.Range["A4"].NumberFormat = "0%";
28+
worksheet.Range["A5"].Number = 0.9;
29+
worksheet.Range["A5"].NumberFormat = "0%";
30+
worksheet.Range["A6"].Number = 0.7;
31+
worksheet.Range["A6"].NumberFormat = "0%";
32+
worksheet.Range["A7"].Number = 0.6;
33+
worksheet.Range["A7"].NumberFormat = "0%";
34+
35+
//Adjust row height and column width of the used range
36+
worksheet.UsedRange.RowHeight = 20;
37+
worksheet.UsedRange.ColumnWidth = 25;
38+
39+
//Apply the first conditional format
40+
IConditionalFormats condition = worksheet.UsedRange.ConditionalFormats;
41+
IConditionalFormat condition1 = condition.AddCondition();
42+
43+
condition1.FormatType = ExcelCFType.CellValue;
44+
condition1.FirstFormula = "300";
45+
condition1.Operator = ExcelComparisonOperator.Less;
46+
condition1.FontColor = ExcelKnownColors.Black;
47+
condition1.BackColor = ExcelKnownColors.Sky_blue;
48+
49+
//Apply the second conditional format
50+
IConditionalFormats condition2 = worksheet.UsedRange.ConditionalFormats;
51+
IConditionalFormat condition3 = condition2.AddCondition();
52+
condition3.FormatType = ExcelCFType.IconSet;
53+
IIconSet iconSet = condition3.IconSet;
54+
iconSet.IconSet = ExcelIconSetType.ThreeTrafficLights1;
55+
56+
//Saving the workbook
57+
FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write);
58+
workbook.SaveAs(outputStream);
59+
outputStream.Dispose();
60+
}
61+
62+
}
63+
64+
}
65+
}
66+
67+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Traffic_lights_icon</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)