0

以下のコードを使用して、Excel で特定の単語を強調表示しています。検索語は xml ファイルにあります。セル内のすべての単語を文字列に取り、それらを分割して検索単語と比較しています。したがって、単語が「can」の場合、can を含むセルが強調表示されます。しかし、問題は、単語が「Can Be」の場合、分割されて強調表示されないことです。この問題を解決する方法はありますか。

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

1 に答える 1