2016-06-13 92 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

相關問題