gpt4 book ai didi

c# - 在Word中设置图表系列颜色

转载 作者:太空宇宙 更新时间:2023-11-03 13:27:08 25 4
gpt4 key购买 nike

我想通过使用 VS 2012 和 word 互操作程序集的 Windows 服务为 Word 2010 图表中的所有系列设置不同的颜色。

我知道如何设置它:

    Series series1.Format.Line.ForeColor.RGB = (int)XlRgbColor.xlDarkGoldenrod;

但我想设置准确的 RGB 值,例如 (111,154,169),而不是一些预定义的颜色(甚至不是 hexValues)。

我正在寻找一些使用 System.Drawing.Color 结构来执行此操作的方法,但没有成功。

最佳答案

尝试:

Series series1.Format.Line.ForeColor.RGB = System.Drawing.Color.FromArgb(111,154,169).ToArgb()

请注意,至少在 Excel 中,R 和 B 值有时会颠倒,因此如果颜色不匹配,请尝试调换 R 和 B 分量。

请参阅另一个线程的引用:Changing an Excel cell's backcolor using hex results in Excel displaying completely different color in the spreadsheet

I finally figured it out, after lots of tests, and it was something really simple. Apparently, Excel's Interop library has a bug and is reversing the Red and Blue values, so instead of passing it a hex of RGB, I need to pass BGR, and suddenly the colors work just fine. I'm amazed that this bug isn't documented anywhere else on the internet.

So if anyone else ever runs into this problem, simply pass Excel values in BGR values. (Or if using Color.FromArgb(), pass in Color.FromArgb(B, G, R))

关于c# - 在Word中设置图表系列颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22038320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com