如何Excel.Range.Interior.Color转换的System.Drawing.Color在C#中? [英] How to convert Excel.Range.Interior.Color to System.Drawing.Color in C#?

查看:167
本文介绍了如何Excel.Range.Interior.Color转换的System.Drawing.Color在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表具有一些背景色的一些细胞。我需要这种颜色在HTML代码,所以我想Excel.Range.Interior.Color转换为RGB格式或的System.Drawing.Color。

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.

这样做之后,我将。正在使用System.Drawing.ColorTranslator.ToHtml(的System.Drawing.Color)来获取颜色html标签,使用

After doing that i would be using System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color) to get color to be used in html tags.

我试图做以下几点:

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误差

But i get an error that i cannot convert System.Double to System.Drawing.Color

推荐答案

由Excel.Range.Interior.Color返回的值是一个颜色的长整型值。

The value returned by Excel.Range.Interior.Color is a long integer value of a color.

例子

'#000000等于0

'#000000 is equal to 0

#FFFFFF等于16777215

'#FFFFFF is equal to 16777215

您需要将十进制值转换为十六进制。从那里,它很容易转换为RGB。 (组到2个字节,并将其转换回十进制):)

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) :)

这篇关于如何Excel.Range.Interior.Color转换的System.Drawing.Color在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆