0

下面是在 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);                                        
            }

        }
    }
}
4

1 回答 1

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); 
}
于 2013-09-11T11:10:16.453 回答