Windows应用程序从doc文件中提取姓名电子邮件ID和联系号码

发布于 2024-10-20 03:47:01 字数 2350 浏览 8 评论 0原文

我想从word文档文件中提取姓名,联系人编号和电子邮件ID,我如何提取请任何人帮助我的代码在这里

public void Opendoc(object file)
        {
            Microsoft.Office.Interop.Word.Application wpp = new Microsoft.Office.Interop.Word.Application();
            object nobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document doc = wpp.Documents.Open(ref file, ref nobj, ref nobj, ref nobj,
                            ref nobj, ref nobj, ref nobj, ref nobj, ref nobj, ref nobj,
                            ref nobj, ref nobj, ref nobj, ref nobj, ref nobj, ref nobj);
            int i = 1;
            List <string> emailCollection = new  List<string>();
            foreach (Microsoft.Office.Interop.Word.Paragraph objParagraph in doc.Paragraphs)
 try
                {
                    string emailaddress = document.Paragraphs[1].Range.Text;
                    emailaddress = EmailExtractot(emailaddress).TrimEnd();
                    if (IsEmail(emailaddress))
                    {
                        emailCollection.Add(emailaddress);
                    }
                }
                catch (Exception ex) { throw ex; } i++;

            } // close document and Quit Word 
            document.Close(ref nullobj, ref nullobj, ref nullobj);

        }

 public const string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
  public static bool IsEmail(string email)
        {
            if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
            else return false;
        }

 private static string EmailExtractot(string orginal)
        {

            int index = orginal.IndexOf('@',' ');
            int beforeEmptySpace = orginal.Substring(0, index).LastIndexOf(' ');
            string spiled = orginal.Substring(index, (orginal.Length - index));
            int afterEmptySpace = spiled.IndexOf(' ');
            string emailAddress = orginal.Substring(beforeEmptySpace + 1, (index - beforeEmptySpace) + afterEmptySpace);
            return emailAddress;
        }

,但此代码不起作用

i want to extract name,contactno and emailid from the word document file how i can extract plz any one help my code is here

public void Opendoc(object file)
        {
            Microsoft.Office.Interop.Word.Application wpp = new Microsoft.Office.Interop.Word.Application();
            object nobj = System.Reflection.Missing.Value;

            Microsoft.Office.Interop.Word.Document doc = wpp.Documents.Open(ref file, ref nobj, ref nobj, ref nobj,
                            ref nobj, ref nobj, ref nobj, ref nobj, ref nobj, ref nobj,
                            ref nobj, ref nobj, ref nobj, ref nobj, ref nobj, ref nobj);
            int i = 1;
            List <string> emailCollection = new  List<string>();
            foreach (Microsoft.Office.Interop.Word.Paragraph objParagraph in doc.Paragraphs)
 try
                {
                    string emailaddress = document.Paragraphs[1].Range.Text;
                    emailaddress = EmailExtractot(emailaddress).TrimEnd();
                    if (IsEmail(emailaddress))
                    {
                        emailCollection.Add(emailaddress);
                    }
                }
                catch (Exception ex) { throw ex; } i++;

            } // close document and Quit Word 
            document.Close(ref nullobj, ref nullobj, ref nullobj);

        }

 public const string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";
  public static bool IsEmail(string email)
        {
            if (email != null) return Regex.IsMatch(email, MatchEmailPattern);
            else return false;
        }

 private static string EmailExtractot(string orginal)
        {

            int index = orginal.IndexOf('@',' ');
            int beforeEmptySpace = orginal.Substring(0, index).LastIndexOf(' ');
            string spiled = orginal.Substring(index, (orginal.Length - index));
            int afterEmptySpace = spiled.IndexOf(' ');
            string emailAddress = orginal.Substring(beforeEmptySpace + 1, (index - beforeEmptySpace) + afterEmptySpace);
            return emailAddress;
        }

but this code not working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣 2024-10-27 03:47:01
    private void button1_Click(object sender, EventArgs e)
    {
        // Reference Microsoft.Office.Interop.Word

        // using System;
        // using System.IO;
        // using System.Windows.Forms;
        // using Microsoft.Office.Interop.Word; 

        MessageBox.Show(GetEmailAddress("C:\\Sample.docx"));
    }

    private string GetEmailAddress(string file)
    {

        string emails = "";

        // Open a doc file.
        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Document document = application.Documents.Open(file);

        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;

            //Extract Emails
            if (document.Words[i].Text.Contains("@"))
            {
                emails += document.Words[i - 1].Text + text + document.Words[i + 1].Text + "; ";
            }
        }
        // Close word.
        application.Quit();
        return emails;
    }
}
    private void button1_Click(object sender, EventArgs e)
    {
        // Reference Microsoft.Office.Interop.Word

        // using System;
        // using System.IO;
        // using System.Windows.Forms;
        // using Microsoft.Office.Interop.Word; 

        MessageBox.Show(GetEmailAddress("C:\\Sample.docx"));
    }

    private string GetEmailAddress(string file)
    {

        string emails = "";

        // Open a doc file.
        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Document document = application.Documents.Open(file);

        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            // Write the word.
            string text = document.Words[i].Text;

            //Extract Emails
            if (document.Words[i].Text.Contains("@"))
            {
                emails += document.Words[i - 1].Text + text + document.Words[i + 1].Text + "; ";
            }
        }
        // Close word.
        application.Quit();
        return emails;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文