简体   繁体   English

如何将System.Drawing.Color分配给Microsoft.Office.Interop.Excel.ColorFormat

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

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. 无法隐式转换类型,一个是Struct,另一个是ColorFormat。

ForeColor.RGB is Int type, I can only get 3 ints from Color.Red.R, Color.Red.G and Color.Red.B. ForeColor.RGB是Int类型,我只能从Color.Red.R,Color.Red.G和Color.Red.B中获得3个int。 How to assign the color I want to ForeColor property? 如何将我想要的颜色分配给ForeColor属性?

As suggested in the comments, use ColorTranslator.ToOle 如注释中所建议,使用ColorTranslator.ToOle

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

Or, entering the RGB values: 或者,输入RGB值:

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 ). ForeColor.RGB可以十六进制表示为0xBBGGRRsource )。 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 在另一个答案中使用ToArgb()的原因ToArgb() ,因为其十六进制表示为0xAARRGGBB

Try RGB property ForeColor.RGB 尝试RGB属性ForeColor.RGB

Eg 例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM