none
range.interior.color , how to set from a html color code

    Question

  • hello all,

    I get the color code from the server as a string and i need to convert the color code into Excel's color.

    for ex. I get the color code from server as ""#ffff99". ( this value is not a constant and it can be different ).

    How do I convert "#ffff99" to excel's known color? my project is in c# and I have tried the following and I do see some colors but the color doesn't match with the html color code. Could you please help? thanks

    col1 = System.Drawing.ColorTranslator.FromHtml(style.backgroundColor);
    rng.Interior.Color = col1.ToArgb();
    kris
    Sunday, November 08, 2009 5:58 AM

Answers

  • Hello Kris.

    Another way is to use Excel.XlRgbColor constants. Please see this answer posted by Geoff Darst: Using Interior.Color. You also could search this forum with key Interior.Color or others [See: Here], and will find more threads which may interest you.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Monday, November 16, 2009 9:34 AM
    Saturday, November 14, 2009 10:32 AM
  • Hi kris!

    The method Color.ToArgb() is not the method that you need to get the corresponding VB color.
    To get around this:
    First, you need to import the Microsoft.VisualBasic DLL into your project.
    Then, call the Information.RGB() method:



    using Microsoft.VisualBasic;

    System.Drawing.Color col1;
    rng.Interior.Color = Information.RGB(col1.R, col1.G, col1.B);

    Monday, November 09, 2009 2:26 AM

All replies

  • Hi kris!

    The method Color.ToArgb() is not the method that you need to get the corresponding VB color.
    To get around this:
    First, you need to import the Microsoft.VisualBasic DLL into your project.
    Then, call the Information.RGB() method:



    using Microsoft.VisualBasic;

    System.Drawing.Color col1;
    rng.Interior.Color = Information.RGB(col1.R, col1.G, col1.B);

    Monday, November 09, 2009 2:26 AM
  • thanks. I could see the correct color if I set BGR way as shown below. Is this a bug?

    col1 = System.Drawing.Color.FromArgb(B, G, R); // for some reason, xl like it bgr way...
                                r.Interior.Color = col1.ToArgb();
    kris
    Monday, November 09, 2009 5:00 PM
  • Hello Kris.

    Another way is to use Excel.XlRgbColor constants. Please see this answer posted by Geoff Darst: Using Interior.Color. You also could search this forum with key Interior.Color or others [See: Here], and will find more threads which may interest you.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bessie Zhao Monday, November 16, 2009 9:34 AM
    Saturday, November 14, 2009 10:32 AM
  • With C# on Excel 2010 this works, without the need to reverse the RGB as said before (must be patched by MS?)

    objExcelWs.Range[objExcelWs.Cells[i, 1], objExcelWs.Cells[i, dLastColumn]].Interior.Color = Color.FromArgb(200, 205, 210);

    Regards,

    Jaimy.


    • Edited by Jaimy.Porter Wednesday, February 12, 2014 3:48 PM added C#
    Wednesday, February 12, 2014 3:47 PM