如何在WebBrowser控件中搜索文本

时间:2012-12-20 12:49:53

标签: c# winforms web

  

Hai,我们有像预览文档一样的要求   windows7 / vista"预览窗格"并且必须实施搜索   Winforms中的功能。为此,我已经加载了Ms office   docuemnts,以及Excel表格和Power Point文档到Webbrowser中   以html格式控制。现在我们需要实现搜索   webbrowser控件中的功能。为此,我们有   使用以下代码实现搜索功能

public bool FindNext(string text, WebBrowser webBrowser1)
        {

            IHTMLDocument2 doc = webBrowser1.Document.DomDocument as IHTMLDocument2;

            IHTMLSelectionObject sel =doc.selection;// (IHTMLSelectionObject)doc.selection;
            IHTMLTxtRange rng = sel.createRange() as IHTMLTxtRange;

            rng.collapse(false); // collapse the current selection so we start from the end of the previous range
            if (rng.findText(text, 1000000, 0))
            {
                rng.select();
                return true;
            }
            else
                FindFirst(text, webBrowser1);
            return false;
        }
  

此代码适用于搜索word中的字符串值   所有出现的文件。但是当它来到Excel和PPt   文档这段代码没有正常工作。它只找到第一个   发生其他事件的字符串无法找到。而   调试我发现,对于word Documents" IHTMLDocument2"宾语   将html内容存储在" innerHTML"和" innerText"和一些   值。但对于Excel,它将文本存储在" innerHTML"只能通过使用   框架和工作表引用到本地临时.html文件   并且它没有任何" innerText"内容显示为null。

  Please provide the solution to search the text in webbrowser control which loaded the html content, that is converted from
     

Excel,PPT到html类型并在webbrowser控件中显示。

     

如果您有任何疑问,请随时问我。

     

谢谢。

0 个答案:

没有答案