[英]How can I change color of text in a cell of MS Excel?
I am using Microsoft.Office.Interop.Excel
library. 我正在使用Microsoft.Office.Interop.Excel
库。
I have a cell with values "Green Red". 我有一个值为“Green Red”的单元格。 What I want is pretty simple. 我想要的很简单。 I want to insert "Green" text to be green and "Red" to be red, like that: 我想将“绿色”文本插入为绿色,将“红色”插入为红色,如下所示:
I am using this code to insert data in cell: 我正在使用此代码在单元格中插入数据:
Excel.Application excelApp = new Excel.Application();
excelApp.Workbooks.Add();
// single worksheet
Excel._Worksheet workSheet = excelApp.ActiveSheet;
for (int startIndex = 0; startIndex < 10; startIndex++)
{
workSheet.Cells[1, (startIndex + 1)] ="Green" + " Red";
}
How to do it? 怎么做?
I've tried this approach , but I do not know what [RangeObject]
is: 我尝试过这种方法 ,但我不知道[RangeObject]
是什么:
[RangeObject].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
Try: 尝试:
workSheet.Cells[1, (i + 1)].Characters[start_pos, len].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
where start_pos
and len
is the part of the string where to apply color. 其中start_pos
和len
是应用颜色的字符串的一部分。
Your use case example: 您的用例示例:
Application excelApp = new Application();
excelApp.Workbooks.Add();
// single worksheet
_Worksheet workSheet = excelApp.ActiveSheet;
string Green = "Green";
string Red = "Red";
for (int start = 0; start < 10; start++)
{
Range ColorMeMine = workSheet.Cells[1, (start + 1)];
ColorMeMine.Value = string.Format("{0} {1}", Green, Red);
ColorMeMine.Characters[0, Green.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
ColorMeMine.Characters[Green.Length + 1, Green.Length + 1 + Red.Length].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.