Skip to content

Commit 2aaa907

Browse files
authored
Merge pull request #228 from SyncfusionExamples/980336-RGBValue
980336-FAQ for how to get the RGB color value for the cell color
2 parents 394833b + be2cb24 commit 2aaa907

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# How to get the RGB values of the cell color using XlsIO?
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.Drawing;
15+
using Syncfusion.XlsIO;
16+
```
17+
18+
Step 5: Include the below code snippet in **Program.cs** to get the RGB values of the cell color using XlsIO.
19+
20+
```csharp
21+
using (ExcelEngine excelEngine = new ExcelEngine())
22+
{
23+
IApplication application = excelEngine.Excel;
24+
application.DefaultVersion = ExcelVersion.Xlsx;
25+
IWorkbook workbook = application.Workbooks.Create(1);
26+
IWorksheet worksheet = workbook.Worksheets[0];
27+
28+
//Apply cell color
29+
worksheet.Range["A1"].CellStyle.ColorIndex = ExcelKnownColors.Custom50;
30+
31+
//Get the RGB values of the cell color
32+
Color color = worksheet.Range["A1"].CellStyle.Color;
33+
byte red = color.R;
34+
byte green = color.G;
35+
byte blue = color.B;
36+
37+
//Print the RGB values
38+
Console.WriteLine($"Red: {red}, Green: {green}, Blue: {blue}");
39+
40+
//Save the workbook
41+
FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
42+
workbook.SaveAs(outputStream);
43+
44+
//Dispose stream
45+
outputStream.Dispose();
46+
}
47+
```
48+
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.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RGBValueCellColor", "RGBValueCellColor\RGBValueCellColor.csproj", "{B693B811-5C3A-40DD-8C56-573409E2F140}"
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+
{B693B811-5C3A-40DD-8C56-573409E2F140}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B693B811-5C3A-40DD-8C56-573409E2F140}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B693B811-5C3A-40DD-8C56-573409E2F140}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B693B811-5C3A-40DD-8C56-573409E2F140}.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 = {814BB82D-5FD2-45C0-9792-C0DE5FDEB2BC}
24+
EndGlobalSection
25+
EndGlobal

FAQ/RGB Value for Cell Color/.NET/RGB Value for Cell Color/RGBValueCellColor/Output/.gitkeep

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.IO;
2+
using Syncfusion.XlsIO;
3+
using Syncfusion.Drawing;
4+
5+
6+
namespace RGBValueCellColor
7+
{
8+
class Program
9+
{
10+
public static void Main(string[] args)
11+
{
12+
using (ExcelEngine excelEngine = new ExcelEngine())
13+
{
14+
IApplication application = excelEngine.Excel;
15+
application.DefaultVersion = ExcelVersion.Xlsx;
16+
IWorkbook workbook = application.Workbooks.Create(1);
17+
IWorksheet worksheet = workbook.Worksheets[0];
18+
19+
//Apply cell color
20+
worksheet.Range["A1"].CellStyle.ColorIndex = ExcelKnownColors.Custom50;
21+
22+
//Get the RGB values of the cell color
23+
Color color = worksheet.Range["A1"].CellStyle.Color;
24+
byte red = color.R;
25+
byte green = color.G;
26+
byte blue = color.B;
27+
28+
//Print the RGB values
29+
Console.WriteLine($"Red: {red}, Green: {green}, Blue: {blue}");
30+
31+
//Save the workbook
32+
FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
33+
workbook.SaveAs(outputStream);
34+
35+
//Dispose stream
36+
outputStream.Dispose();
37+
}
38+
}
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Output\*">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)