【号码分离】从Excel表格、文本、word文档混乱文字中提取分离11位手机号出来,基于WPF的实现方案

当从不同渠道收集到的数据中包含混乱的文字信息时,需要从中提取出有效的手机号。例如,从用户反馈的文本信息、会议记录等文档中提取手机号,以便进行数据整理和分析。

企业可能会有一些历史的文档资料,其中包含客户的相关信息。通过提取手机号,可以将这些信息整合到客户关系管理系统中,完善客户信息,提高客户服务质量。

文本手机邮箱号码处理提取工具地址:

百度网盘: https://pan.baidu.com/s/1xhsv9OsTTOCP1kja1HihOg?pwd=zkc6
腾讯云盘: https://share.weiyun.com/0LE429bX

具体的实现步骤

1. 创建 WPF 项目

打开 Visual Studio,创建一个新的 WPF 应用程序项目。

2. 设计用户界面(XAML)

MainWindow.xaml 中设计一个简单的界面,包含选择文件的按钮、显示提取结果的文本框等。以下是一个示例:

xml

复制代码
<Window x:Class="PhoneNumberExtractor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="手机号提取工具" Height="450" Width="800">
    <Grid>
        <Button Content="选择 Excel 文件" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="150" Click="SelectExcelFile_Click"/>
        <Button Content="选择文本文件" HorizontalAlignment="Left" Margin="20,60,0,0" VerticalAlignment="Top" Width="150" Click="SelectTextFile_Click"/>
        <Button Content="选择 Word 文件" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="150" Click="SelectWordFile_Click"/>
        <TextBox x:Name="ResultTextBox" HorizontalAlignment="Left" Height="300" Margin="200,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="550" IsReadOnly="True"/>
    </Grid>
</Window>
3. 引入必要的 NuGet 包

为了处理不同类型的文件,需要引入相应的 NuGet 包:

  • Excel 文件 :使用 ClosedXML 库,它可以方便地读取 Excel 文件。在 Visual Studio 的 "工具" -> "NuGet 包管理器" -> "管理解决方案的 NuGet 程序包" 中搜索并安装 ClosedXML
  • Word 文件 :使用 Microsoft.Office.Interop.Word,但需要注意这依赖于本地安装的 Microsoft Word 软件。在项目中添加对 Microsoft.Office.Interop.Word 的引用。
4. 编写代码逻辑(C#)

MainWindow.xaml.cs 中实现文件选择和手机号提取的逻辑:

csharp

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using ClosedXML.Excel;
using Microsoft.Office.Interop.Word;

namespace PhoneNumberExtractor
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectExcelFile_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Excel 文件|*.xlsx;*.xls";
            if (dialog.ShowDialog() == true)
            {
                string filePath = dialog.FileName;
                var phoneNumbers = ExtractPhoneNumbersFromExcel(filePath);
                DisplayPhoneNumbers(phoneNumbers);
            }
        }

        private void SelectTextFile_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "文本文件|*.txt";
            if (dialog.ShowDialog() == true)
            {
                string filePath = dialog.FileName;
                var phoneNumbers = ExtractPhoneNumbersFromText(filePath);
                DisplayPhoneNumbers(phoneNumbers);
            }
        }

        private void SelectWordFile_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.Filter = "Word 文件|*.docx;*.doc";
            if (dialog.ShowDialog() == true)
            {
                string filePath = dialog.FileName;
                var phoneNumbers = ExtractPhoneNumbersFromWord(filePath);
                DisplayPhoneNumbers(phoneNumbers);
            }
        }

        private List<string> ExtractPhoneNumbersFromExcel(string filePath)
        {
            var phoneNumbers = new List<string>();
            using (var workbook = new XLWorkbook(filePath))
            {
                foreach (var worksheet in workbook.Worksheets)
                {
                    foreach (var row in worksheet.Rows())
                    {
                        foreach (var cell in row.Cells())
                        {
                            string cellValue = cell.Value.ToString();
                            phoneNumbers.AddRange(ExtractPhoneNumbers(cellValue));
                        }
                    }
                }
            }
            return phoneNumbers;
        }

        private List<string> ExtractPhoneNumbersFromText(string filePath)
        {
            string text = File.ReadAllText(filePath);
            return ExtractPhoneNumbers(text);
        }

        private List<string> ExtractPhoneNumbersFromWord(string filePath)
        {
            var phoneNumbers = new List<string>();
            var application = new Application();
            var document = application.Documents.Open(filePath);
            try
            {
                string content = document.Content.Text;
                phoneNumbers = ExtractPhoneNumbers(content);
            }
            finally
            {
                document.Close();
                application.Quit();
            }
            return phoneNumbers;
        }


        private void DisplayPhoneNumbers(List<string> phoneNumbers)
        {
            ResultTextBox.Text = string.Join(Environment.NewLine, phoneNumbers);
        }
    }
}
5. 运行程序

编译并运行项目,点击相应的按钮选择文件,程序会自动提取其中的 11 位手机号并显示在文本框中。

相关推荐
charlie1145141911 小时前
面向C++程序员的JavaScript 语法实战学习4
开发语言·前端·javascript·学习·函数
夫唯不争,故无尤也1 小时前
Python广播机制:张量的影分身术
开发语言·python
qq_479875431 小时前
X-Macros(3)
java·开发语言
列逍1 小时前
深入理解 C++ 异常:从概念到实战的全面解析
开发语言·c++
java1234_小锋2 小时前
简述Mybatis的插件运行原理?
java·开发语言·mybatis
charlie1145141912 小时前
勇闯前后端Week2:后端基础——HTTP与REST
开发语言·网络·笔记·网络协议·学习·http
玩泥巴的2 小时前
使用.NET 8+ 与飞书API构建组织架构同步服务
c#·.net·二次开发·飞书
福尔摩斯张2 小时前
二维数组详解:定义、初始化与实战
linux·开发语言·数据结构·c++·算法·排序算法
大佬,救命!!!2 小时前
C++函数式策略模式代码练习
开发语言·c++·学习笔记·学习方法·策略模式·迭代加深·多文件编译