Skip to content

Commit 4bf6ee3

Browse files
authored
Merge pull request #208 from SyncfusionExamples/912128-HighlightCellsWithErrors
912128-How to apply conditional formatting to highlight cells with errors in red background using C#?
2 parents 641b8e4 + 4c65bd6 commit 4bf6ee3

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36127.28 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HighLightCellsWithErrors", "HighLightCellsWithErrors\HighLightCellsWithErrors.csproj", "{2640FE48-0576-497D-A510-967D9CF72201}"
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+
{2640FE48-0576-497D-A510-967D9CF72201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{2640FE48-0576-497D-A510-967D9CF72201}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{2640FE48-0576-497D-A510-967D9CF72201}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{2640FE48-0576-497D-A510-967D9CF72201}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {311E5A9F-EE7B-4384-9CAA-B5CC318B459B}
24+
EndGlobalSection
25+
EndGlobal
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>Highlight_cells_with_errors</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>

Use Cases/Conditional Formatting/.NET/Highlight cells with errors/HighLightCellsWithErrors/Output/.gitkeep

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.XlsIO;
2+
3+
class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
using (ExcelEngine excelEngine = new ExcelEngine())
8+
{
9+
IApplication application = excelEngine.Excel;
10+
IWorkbook workbook = application.Workbooks.Create(1);
11+
IWorksheet worksheet = workbook.Worksheets[0];
12+
13+
//Add some data and errors
14+
worksheet.Range["A1"].Text = "Sample Data";
15+
16+
//Creates a #DIV/0! error
17+
worksheet.Range["A2"].Formula = "=1/0";
18+
19+
//Creates a #N/A error
20+
worksheet.Range["A3"].Formula = "=VLOOKUP(\"NonExistent\",B1:C5,2,FALSE)";
21+
22+
//Define the range to apply formatting
23+
IRange range = worksheet.Range["A1:A10"];
24+
25+
//Add conditional formatting to highlight cells with errors
26+
IConditionalFormats conditionalFormats = range.ConditionalFormats;
27+
IConditionalFormat conditionalFormat = conditionalFormats.AddCondition();
28+
29+
//Set format type to ContainsErrors
30+
conditionalFormat.FormatType = ExcelCFType.ContainsErrors;
31+
32+
//Apply red background to cells containing errors
33+
conditionalFormat.BackColor = ExcelKnownColors.Red;
34+
35+
#region Save
36+
//Saving the workbook
37+
FileStream outputStream = new FileStream(Path.GetFullPath("Output.xlsx"), FileMode.Create, FileAccess.Write);
38+
workbook.SaveAs(outputStream);
39+
#endregion
40+
41+
//Dispose streams
42+
outputStream.Dispose();
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)