应用场景
在市场调研过程中,可能会收集到大量的 Excel 表格、文本报告或 Word 文档,其中包含客户的联系方式。通过提取手机号,可以方便后续的市场推广和客户跟进。
当从不同渠道收集到的数据中包含混乱的文字信息时,需要从中提取出有效的手机号。例如,从用户反馈的文本信息、会议记录等文档中提取手机号,以便进行数据整理和分析。
企业可能会有一些历史的文档资料,其中包含客户的相关信息。通过提取手机号,可以将这些信息整合到客户关系管理系统中,完善客户信息,提高客户服务质量。
![](https://i-blog.csdnimg.cn/direct/74d10a9178184d0a8395264251387437.png)
具体的实现步骤
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 位手机号并显示在文本框中。