下面是在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);
}
}
}
}
答案 0 :(得分:0)
您需要在单个单元格上执行操作。在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);
}