How to assign System.Drawing.Color to Microsoft.Office.Interop.Excel.ColorFormat

流过昼夜 提交于 2019-12-13 05:25:18

问题


Sample code as follow,

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor = System.Drawing.Color.Red;

Return an error. Cannot implicitly convert type, one is a Struct, the other is ColorFormat.

ForeColor.RGB is Int type, I can only get 3 ints from Color.Red.R, Color.Red.G and Color.Red.B. How to assign the color I want to ForeColor property?


回答1:


Try RGB property ForeColor.RGB

E.g.

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = System.Drawing.Color.Red.ToArgb();



回答2:


As suggested in the comments, use ColorTranslator.ToOle

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.Red);

Or, entering the RGB values:

Chart.SerialsCollection(1).Point(1).Format.Fill.ForeColor.RGB = ColorTranslator.ToOle(Color.FromArgb(255, 0, 0);

Some additional information, so I'm not just repeating the comments:

ForeColor.RGB can be represented in Hex as 0xBBGGRR (source). Knowing that, you could create the following function:

public int Color_to_RGB(Color color) {
    return (color.B << 16) | (color.G << 8) | Color.R;
}

Use:

Chart.SerialCollection(1).Point(1).Format.Fill.ForeColor.RGB = Color_to_RGB(Color.Red);

The reason ToArgb(), used in the other answer, does not work because its Hex representation is 0xAARRGGBB



来源:https://stackoverflow.com/questions/27518048/how-to-assign-system-drawing-color-to-microsoft-office-interop-excel-colorformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!