我正在使用以下代码突出显示 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");
}
}
}
}