在excel中搜索单词并在C#中突出显示它们

时间:2013-09-11 09:59:40

标签: c# xml excel highlight

我需要在excel中搜索几个单词并突出显示这些单词。将这些单词保存在xml文件中。以下是我正在使用的代码。该范围采用偶数null,这需要花费更多时间并且它读取整行(以字符串形式)而不是读取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; //am getting the whole row but i need to read each word seperately
            if (str == text.ToString())
                {
                range.Font.Bold = 1;
                range.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

此方法查找文本并突出显示excel中的单词

public void FindTextAndChangeColor(string text, Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet)
    {
        Microsoft.Office.Interop.Excel.Range currentFind = null;
        Microsoft.Office.Interop.Excel.Range firstFind = null;

        // Find the first occurrence of the passed-in text

        currentFind = excelWorkSheet.Cells.Find(text, Missing.Value, Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues,
            Microsoft.Office.Interop.Excel.XlLookAt.xlPart, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext,
            false, Missing.Value, Missing.Value);

        while (currentFind != null)
        {
            // Keep track of the first range we find

            if (firstFind == null)
            {
                firstFind = currentFind;
            }
            else if (currentFind.get_Address(Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,
                Missing.Value, Missing.Value) ==
                firstFind.get_Address(Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1,
                Missing.Value, Missing.Value))
            {
                // We didn't move to a new range so we're done

                break;
            }

            // We know our text is in first cell of this range, so we need to narrow down its position

            string searchResult = currentFind.get_Range("A1").Value2.ToString();
            int startPos = searchResult.IndexOf(text);

            // Set the text in the cell to bold

            currentFind.get_Range("A1").Characters[startPos + 1, text.Length].Font.Color = Color.Red;

            // Move to the next find

            currentFind = excelWorkSheet.Cells.FindNext(currentFind);
        }
    }

答案 1 :(得分:0)

根据单元格中的所有字母都突出显示。我想你只想突出你搜索的单元格中的文本。你可以按照这个

Excel.Range val = xlWorkSheet.Cells[18, "C"] as Excel.Range;
string match = "find";
string match2 = val.Value2.ToString();
int index=-1;
//Here apply  string  matching to find index of first letter of matching sub string 
// if the index is not -1  then do following      
        val.get_Characters(index, match.Length).Font.Color =System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);