Problem getting Excel cell cell colors in C #

I am trying to get the background color of some cells in an Excel worksheet in C #. I use the following code for this:

Excel.Range r = (Excel.Range)m_objRange[i, j]; int decValue = int.Parse(r.Interior.Color.ToString()); string hexValue = decValue.ToString("X"); 

So, I get a long decimal value, and then I convert to hex to use it in html code. Now I have a problem getting the right colors. For instance:

Case 1

Actual color is red

The returned decimal value is 255

The offset value of Hex is FF (or 0000FF)

The Corresponding Color I Received - Blue

Case 2

Actual color is blue

The returned decimal value is 16711680

Corresponding Value Hex-FF0000

The Corresponding Color I Received - Red

Case 3

Actual color is green

The returned decimal value is -32768

The corresponding value of Hex-8000

The Corresponding Color I Received - White

Now in case 1, I think I should interpret the hexadecimal value of FF as # FF0000 to get it as red? In case 3, should I interpret the hexadecimal value of 8000 as # 008000 to get green?

Is there a way in which I can get a six-digit hexadecimal value that will be exactly the color that I want? Am I getting the wrong decimal values ​​or am I not converting the decimal numbers to six properly?

And what happens in case 2? Here I get a six-digit hexadecimal value, but this is completely wrong. FF0000 is clearly red, not blue.

+4
source share
2 answers

This work for me:

 int colorNumber = System.Convert.ToInt32(((Range) worksheet.Cells[rowNumber,columnNumber]).Interior.Color); Color color = System.Drawing.ColorTranslator.FromOle(colorNumber); 
+8
source

try the following:

 System.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color); string htmlCol = System.Drawing.ColorTranslator.ToHtml(col); 

(warning, I did not check this)

+1
source

Source: https://habr.com/ru/post/1285821/


All Articles