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

应用场景

在市场调研过程中,可能会收集到大量的 Excel 表格、文本报告或 Word 文档,其中包含客户的联系方式。通过提取手机号,可以方便后续的市场推广和客户跟进。

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

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

具体的实现步骤

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 位手机号并显示在文本框中。

相关推荐
沉到海底去吧Go3 天前
【归属地】批量号码归属地查询按城市高速的分流,基于WPF的解决方案
号码归属地查询按城市高速分流·批量号码归属地查询·高速分流实现批量号码归属地查询·批量手机号码归属地查询方法·号码归属地查询与城市高速分流·分流实现批量号码归属地查询技巧·批量号码归属地查询按城市