[英]How can I change color of text in a cell of MS Excel?
我正在使用Microsoft.Office.Interop.Excel
庫。
我有一個值為“Green Red”的單元格。 我想要的很簡單。 我想將“綠色”文本插入為綠色,將“紅色”插入為紅色,如下所示:
我正在使用此代碼在單元格中插入數據:
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";
}
怎么做?
我嘗試過這種方法 ,但我不知道[RangeObject]
是什么:
[RangeObject].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
嘗試:
workSheet.Cells[1, (i + 1)].Characters[start_pos, len].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
其中start_pos
和len
是應用顏色的字符串的一部分。
您的用例示例:
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.