如何在 C# 中将 Excel.Range.Interior.Color 转换为 System.Drawing.Color?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/945808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Excel.Range.Interior.Color to System.Drawing.Color in C#?
提问by
I have an excel sheet with some cells having some background color. I need this color in html code and hence I want to convert Excel.Range.Interior.Color to RGB format or System.Drawing.Color.
我有一个 excel 表格,其中一些单元格有一些背景颜色。我需要在 html 代码中使用这种颜色,因此我想将 Excel.Range.Interior.Color 转换为 RGB 格式或 System.Drawing.Color。
After doing that i would be using System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color) to get color to be used in html tags.
这样做之后,我将使用 System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color) 来获取要在 html 标签中使用的颜色。
I tried doing following:
我尝试执行以下操作:
Excel.Range r = (Excel.Range)m_objRange[2, 2];
System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(r.Interior.Color);
MessageBox.Show(""+converter.ConvertTo(r.Interior.Color,typeof(System.Drawing.Color)));
But i get an error that i cannot convert System.Double to System.Drawing.Color
但我收到一个错误,我无法将 System.Double 转换为 System.Drawing.Color
采纳答案by jgallant
The value returned by Excel.Range.Interior.Color is a long integer value of a color.
Excel.Range.Interior.Color 返回的值是颜色的长整数值。
Examples:
例子:
'#000000 is equal to 0
'#000000 等于 0
'#FFFFFF is equal to 16777215
'#FFFFFF 等于 16777215
You need to convert the decimal value into Hexadecimal. From there, it is easy to convert to RGB. (Group into 2 octets, and convert back to decimal) :)
您需要将十进制值转换为十六进制。从那里,很容易转换为 RGB。(分组为 2 个八位字节,并转换回十进制):)
回答by yoyoyoyosef
It's much easier to let ColorTranslator do the work for you:
让 ColorTranslator 为您完成工作要容易得多:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);
And when you are writing an Excel project inside Visual Studio with the automatically generated proxy objects (instead of the regular Interop project) you need to cast r.Interior.Color to a double, and then back to an int:
当您在 Visual Studio 中使用自动生成的代理对象(而不是常规的 Interop 项目)编写 Excel 项目时,您需要将 r.Interior.Color 转换为 double,然后再转换回 int:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int)((double) r.Interior.Color));
回答by Zvon
Here is how i do it with Function, simply replacing blue and red byte. Example with usage in vb.net:
这是我如何使用 Function ,只需替换蓝色和红色字节。在 vb.net 中的用法示例:
Imports Microsoft.Office.Interop
Public Class Form1
Dim Ex_Ap As Excel.Application
Dim Ex_Wb As Excel.Workbook
Dim Ex_Ws As Excel.Worksheet
Function Excel_Color_Get(ByVal i As Int32) As Color
Static c As Color
c = Color.FromArgb(i)
Return Color.FromArgb(c.B, c.G, c.R)
End Function
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Ex_Ap = New Excel.Application
Ex_Ap.Visible = True
Ex_Wb = Ex_Ap.Workbooks.Add
Ex_Ws = Ex_Wb.Worksheets(1)
Ex_Ws.Cells(7, 6).Value = "???"
Ex_Ws.Cells(7, 6).interior.color = Color.Pink
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Try
TextBox1.BackColor = Excel_Color_Get(Ex_Ws.Cells(7, 6).interior.color)
Catch ex As Exception
TextBox1.Text = ex.ToString
End Try
End Sub
End Class