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 に答える