We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Can't get the right System.Drawing.Color from Excel Cell foreground

Hi everybody,

In my Excel File, I have cells with different background color (red, blue and green).

I want to get this color and put it in a System.Drawing.Color objet but the color is different. Why?

Here is my code for one cell:
System.Drawing.Color color = workSheet.Cells[1, 1].Style.ForegroundColor;

I made some test to convert the color with the System.Drawing.ColorTranslator with no success.

ex: Red cell give me a System.Drawing.Color =(0, 255, 0, 0) rather then (255, 255, 0, 0).

Any ideas.

Thanks

Marc

Hi Marc,

Thanks for your posting and using Aspose.Cells.

This is correct behavior. In MS-Excel, there is no alpha value for colors. Only Red, Green and Blue values of color exists, therefore when you read Foreground Color from MS-Excel cells, you only get RGB values and Alpha value is always 0. You can set Alpha value to 255 yourself.

For example, the following code is converting Foreground Color RGB value to ARGB value.

C#


//Get foreground color

Color clr = style.ForegroundColor;


//Convert RGB value to ARGB

Color nClr = Color.FromArgb(255, clr.R, clr.G, clr.B);

Shakeel,



Thanks for your quick response.



The red color works fine but I tried other colors and the color is different as expected.



See attached JPG file. I combined the excel file and the result I got with my C# software on the same screenshot.



Do you know why? Is it a question of color palette in Excel.



Thanks



Marc

Hi Marc,

Thanks for your posting and using Aspose.Cells.

If your source file is in xls format, then it could be a issue of palette but if it is in xlsx format, then it will not be issue of palette.

Could you please provide us your source file, so that we could investigate why it is returning different colors than what are shown in Excel.

My Excel file was in .xls format. I converted to an .xlsx format (using save as) but again some colors show differently.



If we are able to get the right colors from an .xlsx format, that will be correct for me.



Thanks

I’m using Aspose.Cells

Version 4.6.0.12
Runtime Version: v1.0.3705

Hi Marc,

Thanks for your posting and using Aspose.Cells.

Please download and use the latest version: Aspose.Cells for .NET (Latest Version) it returns correct colors.

I have tested this issue with the following code and the returned colors are correct as you can see in the debug output.

Please see there is one hidden worksheet in your source file at 0 index, so make sure you are accessing the worksheet by name or with index 1. As you can see in my code, I am accessing the worksheet by name.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\Test.xls”;


Workbook workbook = new Workbook(filePath);

//Accessing worksheet by name
Worksheet worksheet = workbook.Worksheets[“Types tâche”];


Debug.WriteLine(worksheet.Cells[“C7”].GetStyle().ForegroundColor);

Debug.WriteLine(worksheet.Cells[“C8”].GetStyle().ForegroundColor);

Debug.WriteLine(worksheet.Cells[“C9”].GetStyle().ForegroundColor);

Debug.WriteLine(worksheet.Cells[“C10”].GetStyle().ForegroundColor);


Debug Output:
Color [A=0, R=255, G=0, B=0]
Color [A=255, R=191, G=144, B=0]
Color [A=0, R=0, G=176, B=80]
Color [A=0, R=91, G=155, B=213]

Thanks shakeel,

I will make some tests next week.

Sorry for the hidden worksheet. I just forgot to delete it before send it to you. My original Excel file contain a lot of worksheets.

Marc

Hi Marc,

Thanks for your posting and using Aspose.Cells.

Hopefully, you will not encounter any issue with the latest version as we have tested it. If you face any issue, please feel free to post, we will be glad to look into it and help you further.