2016-06-13 90 views
2

我正在开发Excel的vsto插件,我试图将颜色更改为Excel中的注释。如何将System.Drawing.Color从C#转换为Excel.ColorFormat?更改注释颜色

这是我的代码:

Excel.Range activeCell = _application.ActiveCell; 
activeCell.AddComment("some text")); 
activeCell.Comment.Shape.Fill.BackColor = Color.Red; 

我得到的例外是:

无法隐式转换类型 '的System.Drawing.Color' 到“Microsoft.Office。 Interop.Excel.ColorFormat'

我无法找到如何在两种格式之间进行转换。

enter image description here

+0

看一看[本](HTTP:// stackoverflo w.com/questions/27518048/how-to-assign-system-drawing-color-to-microsoft-office-interop-excel-colorformat)。 –

回答

3

一种选择是使用ColorTranslator.ToOle

int oleColor = ColorTranslator.ToOle(Color.Red); 
activeCell.Comment.Shape.Fill.BackColor.RGB = oleColor; 
+0

问题是要求另一种方式转换,所以这个答案是不正确的...我想知道为什么upvoter也没有阅读这个问题...哈哈,也许你有一个克隆 – musefan

+0

啊......好吧,忽略它。将更新答案。 –

+0

感谢@musefan,更新了它。 –

0

试试这个:

activeCell.Comment.Shape.Fill.BackColor = XlRgbColor.rgbRed; 

还是这个(编辑:假):

activeCell.Comment.Shape.Fill.BackColor.RGB = Color.FromRgb(255,0,0); 
+1

我不相信这会起作用。 'Color.FromRgb'仍然会返回一个'Color'对象,'RGB'不会接受它。如果确实如此,则OP可以按照原始尝试使用“Color.Red”。 – musefan

相关问题