我有一个 Excel 工作表,其中一些单元格具有一些背景颜色。我在 html 代码中需要这种颜色,因此我想将 Excel.Range.Interior.Color 转换为 RGB 格式或 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
最佳答案
让 ColorTranslator 为您完成工作要容易得多:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int) r.Interior.Color);
当您在 Visual Studio 中使用自动生成的代理对象(而不是常规的 Interop 项目)编写 Excel 项目时,您需要将 r.Interior.Color 转换为 double,然后再转换回 int:
System.Drawing.Color col = System.Drawing.ColorTranslator.FromOle((int)((double) r.Interior.Color));
关于c# - 如何在 C# 中将 Excel.Range.Interior.Color 转换为 System.Drawing.Color?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/945808/