Excel字体颜色 [英] Excel Font Color
本文介绍了Excel字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取字体颜色,并试图反转设置颜色的方式,但是我收到了无效的强制转换异常.我不确定我做错了什么.这是我所拥有的:
Hi,
I''m trying to obtain the font color and I tried to reverse how I set the color, however I''m getting an invalid cast exception. I''m not sure what I did wrong. Here''s what I have:
public System.Drawing.Color GetRangeFontColor(string range)
{
System.Drawing.Color color;
Excel.Range workingRangeCells = excelWorksheet.get_Range(range, Type.Missing);
color = (System.Drawing.Color) workingRangeCells.Font.Color;
return color;
}
public void SetRangeFontColor(string range, System.Drawing.Color color)
{
Excel.Range workingRangeCells = excelWorksheet.get_Range(range, Type.Missing);
workingRangeCells.Font.Color = System.Drawing.ColorTranslator.ToOle(color);
return;
}
谢谢你的帮助! :)
Thank you for help! :)
推荐答案
使用 ColorTranslator [ ^ ]类,而不是类型转换.您必须使用其 FromOle [ ^ ]方法 OLE 颜色值更改为 GDI +Color
. :)
[更新]
ColorTranslator.FromOle
方法需要一个整数作为参数.因此,您需要将Range.Font.Color
转换为int
.而不是使用类型转换,请尝试使用 Convert.ToInt32 [
Use the ColorTranslator[^] class instead of typecast. You''ll have to use its FromOle[^] method in order to convert the OLE color value to GDI+Color
. :)
[UPDATE]
ColorTranslator.FromOle
method requires an integer as parameter. So you''ll need to convert fromRange.Font.Color
toint
. Instead of using a typecast, try with Convert.ToInt32[^] method.
I''ve made a test and the following code works fine for me:
Excel.Range workingRangeCells = excelWorksheet.get_Range(range, Type.Missing);
System.Drawing.Color color = System.Drawing.ColorTranslator.FromOle(Convert.ToInt32(workingRangeCells.Font.Color));
[/UPDATE]
:)
[/UPDATE]
:)
我已经尝试了您的建议,现在尝试将workingRangeCells.Font.Color转换为int时收到无效的指定转换. workingRangeCells.Font.Color在我强制转换之前返回了一个对象. ColorTranslator.FromOle需要一个(int).
Hi,
I have tried what you suggested and now I''m receiving a specified cast not valid when trying to cast workingRangeCells.Font.Color to an int. workingRangeCells.Font.Color returned an Object before I casted it. ColorTranslator.FromOle wants an (int).
color = ColorTranslator.FromOle((int) workingRangeCells.Font.Color);
谢谢
Thank you,
这篇关于Excel字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文