我有一个excel工作表,其中一些单元格具有一些背景颜色。我需要这种颜色的html代码,因此我想转换为Excel.Range.Interior.Color格式或System.Drawing.Color。
这样做之后,我将使用System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color)来获取要在html标记中使用的颜色。
我尝试了以下操作:
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)));但是我得到一个错误,我无法将System.Double转换为System.Drawing.Color
发布于 2009-06-03 16:58:33
Excel.Range.Interior.Color返回的值是颜色的长整型值。
示例
'#000000等于0
'#FFFFFF等于16777215
您需要将十进制值转换为十六进制。从那里,很容易转换为RGB。(分组为2个八位字节,并转换回十进制) :)
发布于 2009-06-08 13:07:38
让ColorTranslator为您做这项工作要容易得多:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);当您在Visual Studio中使用自动生成的代理对象(而不是常规的互操作项目)编写Excel项目时,您需要将r.Interior.Color转换为double,然后再转换为int:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int)((double) r.Interior.Color));发布于 2020-01-15 07:46:49
这就是我如何使用函数,简单地替换蓝色和红色字节。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 Classhttps://stackoverflow.com/questions/945808
复制相似问题