From 8ee34dcf2e3c2dd94303211b4ff608ae95ce3963 Mon Sep 17 00:00:00 2001 From: KarthikaSF4773 Date: Wed, 30 Apr 2025 17:21:42 +0530 Subject: [PATCH] 261512-TrafficLightFaqExample --- .../.NET/Traffic lights icon/README.md | 70 +++++++++++++++++++ .../Traffic lights icon.sln | 22 ++++++ .../Traffic lights icon/Output/.gitkeep | 0 .../Traffic lights icon/Program.cs | 67 ++++++++++++++++++ .../Traffic lights icon.csproj | 21 ++++++ 5 files changed, 180 insertions(+) create mode 100644 FAQ/Conditional Formatting/.NET/Traffic lights icon/README.md create mode 100644 FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon.sln create mode 100644 FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Output/.gitkeep create mode 100644 FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Program.cs create mode 100644 FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Traffic lights icon.csproj diff --git a/FAQ/Conditional Formatting/.NET/Traffic lights icon/README.md b/FAQ/Conditional Formatting/.NET/Traffic lights icon/README.md new file mode 100644 index 00000000..fbf6d422 --- /dev/null +++ b/FAQ/Conditional Formatting/.NET/Traffic lights icon/README.md @@ -0,0 +1,70 @@ +# How to set traffic lights icon in Excel conditional formatting using C#? + +Step 1: Create a New C# Console Application Project. + +Step 2: Name the Project. + +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). + +Step 4: Include the following namespaces in the **Program.cs** file. + +```csharp +using System; +using System.IO; +using Syncfusion.XlsIO; +``` + +Step 5: Include the below code snippet in **Program.cs** to set traffic lights icon in Excel conditional formatting using C# +```csharp +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Excel2013; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet worksheet = workbook.Worksheets[0]; + + //Add data and formatting to the worksheet + worksheet.Range["A1"].Text = "Traffic Lights"; + + //Add percentage values to cells A2 to A7 and format them as percentages + worksheet.Range["A2"].Number = 0.95; + worksheet.Range["A2"].NumberFormat = "0%"; + worksheet.Range["A3"].Number = 0.5; + worksheet.Range["A3"].NumberFormat = "0%"; + worksheet.Range["A4"].Number = 0.1; + worksheet.Range["A4"].NumberFormat = "0%"; + worksheet.Range["A5"].Number = 0.9; + worksheet.Range["A5"].NumberFormat = "0%"; + worksheet.Range["A6"].Number = 0.7; + worksheet.Range["A6"].NumberFormat = "0%"; + worksheet.Range["A7"].Number = 0.6; + worksheet.Range["A7"].NumberFormat = "0%"; + + //Adjust row height and column width of the used range + worksheet.UsedRange.RowHeight = 20; + worksheet.UsedRange.ColumnWidth = 25; + + //Apply the first conditional format + IConditionalFormats condition = worksheet.UsedRange.ConditionalFormats; + IConditionalFormat condition1 = condition.AddCondition(); + + condition1.FormatType = ExcelCFType.CellValue; + condition1.FirstFormula = "300"; + condition1.Operator = ExcelComparisonOperator.Less; + condition1.FontColor = ExcelKnownColors.Black; + condition1.BackColor = ExcelKnownColors.Sky_blue; + + //Apply the second conditional format + IConditionalFormats condition2 = worksheet.UsedRange.ConditionalFormats; + IConditionalFormat condition3 = condition2.AddCondition(); + condition3.FormatType = ExcelCFType.IconSet; + IIconSet iconSet = condition3.IconSet; + iconSet.IconSet = ExcelIconSetType.ThreeTrafficLights1; + + //Saving the workbook + FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + outputStream.Dispose(); +} +``` + diff --git a/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon.sln b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon.sln new file mode 100644 index 00000000..ea4458a4 --- /dev/null +++ b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35506.116 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Traffic lights icon", "Traffic lights icon\Traffic lights icon.csproj", "{E5911C99-3E19-4A51-906E-6E97D116A4AE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E5911C99-3E19-4A51-906E-6E97D116A4AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5911C99-3E19-4A51-906E-6E97D116A4AE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5911C99-3E19-4A51-906E-6E97D116A4AE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E5911C99-3E19-4A51-906E-6E97D116A4AE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Output/.gitkeep b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Program.cs b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Program.cs new file mode 100644 index 00000000..41b530cb --- /dev/null +++ b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Program.cs @@ -0,0 +1,67 @@ +using System; +using System.IO; +using Syncfusion.XlsIO; + +namespace Traffic_Light +{ + class Program + { + static void Main(string[] args) + { + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Excel2013; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet worksheet = workbook.Worksheets[0]; + + //Add data and formatting to the worksheet + worksheet.Range["A1"].Text = "Traffic Lights"; + + //Add percentage values to cells A2 to A7 and format them as percentages + worksheet.Range["A2"].Number = 0.95; + worksheet.Range["A2"].NumberFormat = "0%"; + worksheet.Range["A3"].Number = 0.5; + worksheet.Range["A3"].NumberFormat = "0%"; + worksheet.Range["A4"].Number = 0.1; + worksheet.Range["A4"].NumberFormat = "0%"; + worksheet.Range["A5"].Number = 0.9; + worksheet.Range["A5"].NumberFormat = "0%"; + worksheet.Range["A6"].Number = 0.7; + worksheet.Range["A6"].NumberFormat = "0%"; + worksheet.Range["A7"].Number = 0.6; + worksheet.Range["A7"].NumberFormat = "0%"; + + //Adjust row height and column width of the used range + worksheet.UsedRange.RowHeight = 20; + worksheet.UsedRange.ColumnWidth = 25; + + //Apply the first conditional format + IConditionalFormats condition = worksheet.UsedRange.ConditionalFormats; + IConditionalFormat condition1 = condition.AddCondition(); + + condition1.FormatType = ExcelCFType.CellValue; + condition1.FirstFormula = "300"; + condition1.Operator = ExcelComparisonOperator.Less; + condition1.FontColor = ExcelKnownColors.Black; + condition1.BackColor = ExcelKnownColors.Sky_blue; + + //Apply the second conditional format + IConditionalFormats condition2 = worksheet.UsedRange.ConditionalFormats; + IConditionalFormat condition3 = condition2.AddCondition(); + condition3.FormatType = ExcelCFType.IconSet; + IIconSet iconSet = condition3.IconSet; + iconSet.IconSet = ExcelIconSetType.ThreeTrafficLights1; + + //Saving the workbook + FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + outputStream.Dispose(); + } + + } + + } +} + + diff --git a/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Traffic lights icon.csproj b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Traffic lights icon.csproj new file mode 100644 index 00000000..ac78d148 --- /dev/null +++ b/FAQ/Conditional Formatting/.NET/Traffic lights icon/Traffic lights icon/Traffic lights icon.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Traffic_lights_icon + enable + enable + + + + + + + + + Always + + + +