How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color

五迷三道 提交于 2020-01-04 06:07:48

问题


I'm developing a vsto addin for Excel, and I'm trying to change the color to the comments in Excel.

This is the code that I have:

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

The exception I'm getting is:

Cannot implicitly convert type 'System.Drawing.Color' to 'Microsoft.Office.Interop.Excel.ColorFormat'

I cannot find how to make a conversion between the two formats.


回答1:


One option is to use ColorTranslator.ToOle

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



回答2:


Try this:

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

Or this(EDIT: False):

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


来源:https://stackoverflow.com/questions/37786008/how-to-convert-from-system-drawing-color-to-excel-colorformat-in-c-change-comm

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