[英]Fine Grained Cell Coloring with Excel and .NET
Microsoft提供了.NET API以编程方式设置单元格颜色
Excel.Range rng2 = this.Application.get_Range("A1");
rng2.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
有没有办法在两种颜色内
是的,您可以为字符的颜色设置颜色。 你不能为背景。 背景着色只能在整个单元格上进行
这是一个代码段(省略了XlColors枚举的定义)
private static readonly Dictionary<string, XlColors> WordColoring = new Dictionary<string, XlColors>
{
{"linksaf", XlColors.Red},
{"schuin links", XlColors.Red},
{"links aanhouden", XlColors.Red},
{"rechtsaf", XlColors.Greenish},
{"schuin rechts", XlColors.Greenish},
{"rechts aanhouden", XlColors.Greenish},
{"rechtdoor", XlColors.Blue}
};
public void TekstFormatter(Range cell)
{
string text = (cell.Value2 ?? "");
foreach (KeyValuePair<string, XlColors> colorPair in WordColoring)
{
int pos = text.IndexOf(colorPair.Key, StringComparison.OrdinalIgnoreCase);
if (pos != -1)
{
Characters chars = cell.Characters[pos + 1, colorPair.Key.Length];
chars.Font.Color = colorPair.Value;
chars.Font.Bold = true;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.