【号码分离】从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 位手机号并显示在文本框中。

相关推荐
Highcharts.js2 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
头发还在的女程序员3 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
爱写代码的小朋友5 小时前
从零开始学 Win32 API:C++ 窗口编程实战(VS Code + MinGW-w64 命令行详解)
开发语言·c++
果汁华5 小时前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
小小晓.5 小时前
C++:语句和作用域
开发语言·c++
wanderist.6 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
海天鹰7 小时前
PHP上传文件
android·开发语言·php
Yeauty8 小时前
渲染成图再 CLI 拼接,还是进程内直推?Rust 帧到视频的两条路
开发语言·rust·音视频
geovindu9 小时前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
库克克10 小时前
【C++】set 与multiset
开发语言·c++