在c#windows中突出显示excel中的单词

时间:2013-11-13 06:08:57

标签: c# windows excel highlight

使用以下代码突出显示excel中的特定单词。搜索词位于xml文件中。我将单元格中的所有单词都放入一个字符串中,并将它们拆分以与搜索单词进行比较。因此,如果一个单词是“can”,则包含can的单元格将被突出显示。但问题是当这个词是“可以”然后它被分裂而不是突出显示。有没有办法解决这个问题。

try
{
    string[] arr = XDocument.Load(xmlSource).Descendants(nodeString)
     .Select(element => element.Value).ToArray();

    string str;
    int rCnt = 0;
    int cCnt = 0;
    for (int x = 1; x <= count; x++)
    {
        Excel.Worksheet xlWorkSheet4;
        Excel.Range range;
        xlWorkSheet4 = (Excel.Worksheet)doc2.Worksheets.get_Item(x);
        Excel.Range last3 = xlWorkSheet4.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
        range = xlWorkSheet4.get_Range("A1", last3);
        for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
        {
            for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
            {
                if (range.Cells[rCnt, cCnt].Value2 is string)
                {
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    if (str == null)
                    {
                        Console.WriteLine("null");
                    }
                    else
                    {
                        str.Replace("\\", "");
                        string[] words = str.Split(' ');
                        foreach (string arrs in arr)
                        {
                            foreach (string word in words)
                            {
                                if (word == arrs)
                                {

                                    var cell = (range.Cells[rCnt, cCnt] as Excel.Range);

                                    cell.Font.Bold = 1;
                                    cell.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("not string");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

     for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
          {
             for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                 {
                    if (range.Cells[rCnt, cCnt].Value2 is string)
                       {
                          str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                          if (str == null)
                          {
                            Console.WriteLine("null");
                          }
                          else
                          {
                            // str.Replace("\\", "");
                            //  string[] words = str.Split(' ');
                            foreach (string arrs in arr)
                            {
                               if (str.Contains(arrs))
                               {
                                 //foreach (string word in words)
                                 //{
                                 //    if (word == arrs)
                                 //    {
                                 var cell = (range.Cells[rCnt, cCnt] as Excel.Range);
                                 cell.Font.Bold = 1;
                                 cell.Font.Color= System.Drawing.ColorTranslator
                                                  .ToOle(System.Drawing.Color.Red);

                                       }

                               }
                           //  }
                             //}
                     }
 }