0

我需要在 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);
                }
            }
        }
    }
4

2 回答 2

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);
        }
    }
于 2016-01-08T11:09:00.140 回答
0

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);
于 2013-09-13T06:49:23.927 回答