使用C#从ASP.NET中的文件中搜索文本 [英] Search Text From file in ASP.NET using C#

查看:93
本文介绍了使用C#从ASP.NET中的文件中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想从文件中搜索文本(可以是姓名,电子邮件,电话号码).

我的ASP.NET页面上有一个File Upload控件和一个Button.用户可以选择任何格式的文件,例如Word,PDF,text,rtf.无论文件格式如何,我都希望从文件中搜索并获取这些详细信息.即,当用户单击按钮时,我应该执行搜索操作并显示那些详细信息,可能在网格视图中.如何使用C#...实现此目标?

谢谢和问候
Karthik Harve

Hi all,
I want to search a text (can be name, email, phone number) from a file.

I have a File Upload control and a Button on my ASP.NET page. User may select a file of any format say Word,PDF,text,rtf. Irrespective of the file format, I want to search and fetch those details from the file. i.e, as and when user clicks on the button, I am supposed to perform the search operation and display those details, may be on a grid view. How can I achieve this using C#...?

Thanks and regards
Karthik Harve

推荐答案

如果仅打开文本和rtf文件并以文本模式读取文件,则可以使用正则表达式搜索文本,但其他文件类型可以二进制形式打开模式,因此您无法通过此方法搜索文本.其他可能的解决方案是您应根据文件格式打开文件,即使用pdf库搜索pdf文件,使用oledb搜索单词中的文本,类似于文件格式一样.
You can search text using regular expression in case of text and rtf files just open and read file in text mode but other file types can be opened in binary mode so you can''t search text by this method. other possible solutions are you should open file according to file format i.e use pdf library to search in pdf files, use oledb to search text in word,excel like file formats.


(仅在文档文件中搜索)
希望我的代码对您有帮助
(search only in doc file)
hope my code will help u
Public string url;
protected void Button3_Click(object sender, EventArgs e)
{
    Process[] ap = Process.GetProcesses();
    foreach (Process p in ap)
    {
        if (p.ProcessName == "WINWORD")
        {
            p.Kill();
        }
    }
    StringBuilder sb = new StringBuilder();
    StringBuilder flenme = new StringBuilder();
    ArrayList li = new ArrayList();
    string pat = @"~\uploads";
    string[] filename = Directory.GetFiles(Server.MapPath(pat));
    foreach (var r in filename)
    {
        string[] split = Regex.Split(r, @"\\");
        li.Add(split [split .Length - 1]);
    }
    foreach (var j in li)
    {
        object file = Server.MapPath(pat) + "\\" + j.ToString();
        object nullobj = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document doc = wordapp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        string result1 = doc.Content.Text.Trim();
        string result2 = Regex.Replace(result1, @"[\r\a]", " ");
        string result = Regex.Replace(result2, @"\s+", " ");
        char[] del = { ' ' };
        string[] SplittedResult = result.Split(del);
        doc.Close();
        url = "Default3.aspx?" + TextBox1.Text;
        foreach (string i in SplittedResult)
        {
            if (TextBox1.Text.ToLower() == i.ToLower())
            {
                sb.AppendLine("<span filename="" + j.ToString() + "" style="color: #FF0000">" + j.ToString() + "             </span><br />" + result + "<hr />");
                sb.Append("?//_\\??");
                break;
            }
        }
    }
    Session["Result"] = sb;
    if (Session["Result"].ToString() != "" && Session["Result"] != null)
    {
        Response.Write("<script language=javascript>window.open('" + url + "')</script>");
        //Response.Redirect(uri);
    }
    else
    {
        Literal1.Text = "No match Found";
    }
}


在搜索结果页面上


on search result page

protected void Page_Load(object sender, EventArgs e)
  {
      if (Session["Result"] != null)
      {
          string sessionResult = Session["Result"].ToString();
          string FormatedResult = Regex.Replace(sessionResult , @"[\r\n]", " ");
          string FinalResult= FormatedResult .Replace("?//_\\??", "<br />");
          Response.Write(FinalResult);

      }
  }


这篇关于使用C#从ASP.NET中的文件中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆