在 c# 中突出显示 Excel 中的搜索词
本文关键字:搜索 Excel 显示 | 更新日期: 2023-09-27 18:31:04
下面是在excel中搜索单词并突出显示它们的代码。但问题是整个范围被突出显示而不是特定的单词。请帮忙
for (int i = 1; i < xmlnode.Count; i++)
{
XmlAttributeCollection xmlattrc = xmlnode[i].Attributes;
object text = xmlnode[i].FirstChild.InnerText;
string str;
int rCnt = 0;
int cCnt = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet1;
Excel.Range range;
xlWorkSheet1 = (Excel.Worksheet)doc1.Worksheets.get_Item(1);
range = xlWorkSheet1.get_Range("A1","A10");
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
string[] words = str.Split(' ');
if (str == text.ToString())
{
range.Font.Bold = 1;
range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}
}
}
}
您需要对单个单元格执行操作。在 if 语句中尝试以下代码:
if (str == text.ToString())
{
var cell = (range.Cells[rCnt, cCnt] as Excel.Range);
cell.Font.Bold = 1;
cell.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
}