简体   繁体   English

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

[英]search for words in excel and highlight them in C#

i need to search for few words in excel and highlight those. 我需要在excel中搜索一些单词并突出显示这些单词。 Have kept the words in an xml file. 将单词保存在xml文件中。 Below is the code am using. 下面是正在使用的代码。 The range takes even null which is taking more time and it reads the whole line(in string) instead of reading each word in excel. 该范围甚至为null,这将花费更多时间,并且它读取整行(以字符串形式),而不是读取excel中的每个单词。 Pls help 请帮助

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

This method finds the text and highlight the word in excel 此方法在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);
        }
    }

According to that all the letters in the cell are highlighted. 据此,该单元格中的所有字母都突出显示。 I think you want to highlight only the text in cell you searched. 我认为您只想突出显示搜索单元格中的文本。 You can follow this 你可以按照这个

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

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

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