How can I help you?
How to get RGB values of a cell’s background color?
18 Dec 20254 minutes to read
The following examples show how to get RGB values of a cell’s background color in C# (cross-platform and Windows-specific) and VB.NET.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Apply cell color
worksheet.Range["A1"].CellStyle.ColorIndex = ExcelKnownColors.Custom50;
//Get the RGB values of the cell color
Color color = worksheet.Range["A1"].CellStyle.Color;
byte red = color.R;
byte green = color.G;
byte blue = color.B;
//Print the RGB values
Console.WriteLine($"Red: {red}, Green: {green}, Blue: {blue}");
//Save the workbook
workbook.SaveAs(Path.GetFullPath("Output/Output.xlsx"));
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Apply cell color
worksheet.Range["A1"].CellStyle.ColorIndex = ExcelKnownColors.Custom50;
//Get the RGB values of the cell color
Color color = worksheet.Range["A1"].CellStyle.Color;
byte red = color.R;
byte green = color.G;
byte blue = color.B;
//Print the RGB values
Console.WriteLine($"Red: {red}, Green: {green}, Blue: {blue}");
//Save the workbook
workbook.SaveAs("Output.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Apply cell color
worksheet.Range("A1").CellStyle.ColorIndex = ExcelKnownColors.Custom50
'Get the RGB values of the cell color
Dim cellColor As Color = worksheet.Range("A1").CellStyle.Color
Dim red As Byte = cellColor.R
Dim green As Byte = cellColor.G
Dim blue As Byte = cellColor.B
'Print the RGB values
Console.WriteLine($"Red: {red}, Green: {green}, Blue: {blue}")
'Save the workbook
workbook.SaveAs("Output.xlsx")
End UsingA complete working example to get RGB values of a cell’s background color is available on this GitHub page.