简体   繁体   English

在C#Windows中突出显示excel中的单词

[英]Highlight words in excel in c# windows

Am using the below code to highlight specific words in excel. 我正在使用以下代码突出显示excel中的特定单词。 The search words are in an xml file. 搜索词在xml文件中。 Am taking all the words in a cell into a string and am splitting them to compare it with search words. 我将一个单元格中的所有单词都分成一个字符串,然后将其拆分以与搜索单词进行比较。 So if a word is "can" then the cell containing can gets highlighted. 因此,如果单词是“罐头”,则包含罐头的单元格会突出显示。 But the problem is when the word is "Can Be" then its getting splitted and not highlighting. 但是问题是当单词“可以是”时,它会被拆分而不突出显示。 Is there any way to come over this problem. 有什么办法可以解决这个问题。

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");
                }
            }
        }
    }
     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);

                                       }

                               }
                           //  }
                             //}
                     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM