【运营商查询】批量手机号码归属地和手机运营商高速查询分类,按省份城市,按运营商移动联通电信快速分类导出Excel表格,基于WPF的实现方案

发布于:2025-05-19 ⋅ 阅读:(14) ⋅ 点赞:(0)

WPF手机号码归属地批量查询与分类导出方案

应用场景

  1. ​市场营销​​:企业根据手机号码归属地进行精准营销,按城市或省份分类制定针对性推广策略
  2. ​客户管理​​:快速对客户手机号码进行归属地分类,便于后续客户关系管理
  3. ​数据分析​​:对大量手机号码进行批量查询和分类,支持导出为表格进行进一步分析
  4. ​呼叫中心​​:在外呼系统中识别客户归属地,优化呼叫策略

界面设计

<Window x:Class="PhoneQueryApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="手机号码归属地查询系统" Height="600" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <!-- 操作区域 --> <StackPanel Grid.Row="0" Margin="10" Orientation="Horizontal"> <Button Content="导入号码" Click="ImportButton_Click" Width="100" Margin="5"/> <Button Content="开始查询" Click="QueryButton_Click" Width="100" Margin="5"/> <Button Content="导出Excel" Click="ExportButton_Click" Width="100" Margin="5"/> <ComboBox x:Name="cmbQueryMode" Width="120" Margin="5" SelectedIndex="0"> <ComboBoxItem Content="本地查询"/> <ComboBoxItem Content="联网查询"/> <ComboBoxItem Content="本地+联网"/> </ComboBox> </StackPanel> <!-- 数据显示区域 --> <DataGrid x:Name="dgResults" Grid.Row="1" Margin="10" AutoGenerateColumns="False" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="手机号码" Binding="{Binding PhoneNumber}" Width="120"/> <DataGridTextColumn Header="省份" Binding="{Binding Province}" Width="100"/> <DataGridTextColumn Header="城市" Binding="{Binding City}" Width="100"/> <DataGridTextColumn Header="运营商" Binding="{Binding Operator}" Width="100"/> </DataGrid.Columns> </DataGrid> <!-- 状态区域 --> <StatusBar Grid.Row="2"> <StatusBarItem> <TextBlock x:Name="txtStatus" Text="就绪"/> </StatusBarItem> <StatusBarItem> <ProgressBar x:Name="pbProgress" Width="200" Height="20"/> </StatusBarItem> </StatusBar> </Grid> </Window>

详细代码实现

1. 数据模型

public class PhoneInfo { public string PhoneNumber { get; set; } public string Province { get; set; } public string City { get; set; } public string Operator { get; set; } }

2. 主窗口代码

using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace PhoneQueryApp { public partial class MainWindow : Window { private List<PhoneInfo> phoneInfos = new List<PhoneInfo>(); public MainWindow() { InitializeComponent(); } private void ImportButton_Click(object sender, RoutedEventArgs e) { var openFileDialog = new Microsoft.Win32.OpenFileDialog { Filter = "文本文件|*.txt|Excel文件|*.xlsx;*.xls|所有文件|*.*" }; if (openFileDialog.ShowDialog() == true) { try { var filePath = openFileDialog.FileName; var extension = Path.GetExtension(filePath).ToLower(); if (extension == ".txt") { var lines = File.ReadAllLines(filePath); phoneInfos = lines.Select(line => new PhoneInfo { PhoneNumber = line.Trim() }).ToList(); } else if (extension == ".xlsx" || extension == ".xls") { // 使用EPPlus或Interop.Excel读取Excel文件 // 这里简化为读取第一列 var excelApp = new Application(); var workbook = excelApp.Workbooks.Open(filePath); var worksheet = (Worksheet)workbook.Sheets[1]; var range = worksheet.UsedRange; phoneInfos = new List<PhoneInfo>(); for (int i = 1; i <= range.Rows.Count; i++) { var cellValue = ((Range)range.Cells[i, 1]).Value2?.ToString(); if (!string.IsNullOrEmpty(cellValue)) { phoneInfos.Add(new PhoneInfo { PhoneNumber = cellValue.Trim() }); } } workbook.Close(false); excelApp.Quit(); } dgResults.ItemsSource = phoneInfos; txtStatus.Text = $"已导入 {phoneInfos.Count} 个号码"; } catch (Exception ex) { MessageBox.Show($"导入失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } private async void QueryButton_Click(object sender, RoutedEventArgs e) { if (phoneInfos.Count == 0) { MessageBox.Show("请先导入手机号码", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } pbProgress.Maximum = phoneInfos.Count; pbProgress.Value = 0; txtStatus.Text = "正在查询..."; var queryMode = ((ComboBoxItem)cmbQueryMode.SelectedItem).Content.ToString(); try { await Task.Run(() => { for (int i = 0; i < phoneInfos.Count; i++) { var phoneInfo = phoneInfos[i]; // 本地查询优先 if (queryMode == "本地查询" || queryMode == "本地+联网") { var localResult = QueryLocal(phoneInfo.PhoneNumber); if (localResult != null) { phoneInfo.Province = localResult.Province; phoneInfo.City = localResult.City; phoneInfo.Operator = localResult.Operator; } else if (queryMode == "本地+联网") { var onlineResult = QueryOnline(phoneInfo.PhoneNumber).Result; if (onlineResult != null) { phoneInfo.Province = onlineResult.Province; phoneInfo.City = onlineResult.City; phoneInfo.Operator = onlineResult.Operator; } } } else if (queryMode == "联网查询") { var onlineResult = QueryOnline(phoneInfo.PhoneNumber).Result; if (onlineResult != null) { phoneInfo.Province = onlineResult.Province; phoneInfo.City = onlineResult.City; phoneInfo.Operator = onlineResult.Operator; } } Dispatcher.Invoke(() => { pbProgress.Value = i + 1; txtStatus.Text = $"正在查询... ({i + 1}/{phoneInfos.Count})"; }); } }); dgResults.Items.Refresh(); txtStatus.Text = $"查询完成,共 {phoneInfos.Count} 个号码"; } catch (Exception ex) { MessageBox.Show($"查询失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); txtStatus.Text = "查询失败"; } } private PhoneInfo QueryLocal(string phoneNumber) { // 这里实现本地数据库查询逻辑 // 可以使用SQLite或内置的号码段数据库 // 返回null表示本地查询失败 // 示例代码 - 实际应替换为真实的本地查询逻辑 if (phoneNumber.StartsWith("138")) { return new PhoneInfo { Province = "北京", City = "北京", Operator = "移动" }; } return null; } private async Task<PhoneInfo> QueryOnline(string phoneNumber) { // 使用第三方API查询 try { using (var client = new HttpClient()) { // 替换为实际的API密钥和URL var apiKey = "your_api_key_here"; var apiUrl = "https://eolink.o.apispace.com/teladress/teladress"; client.DefaultRequestHeaders.Add("X-APISpace-Token", apiKey); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("mobile", phoneNumber) }); var response = await client.PostAsync(apiUrl, content); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); // 解析JSON响应 // 示例代码 - 实际应替换为真实的API响应解析逻辑 return new PhoneInfo { Province = "上海", City = "上海", Operator = "联通" }; } } } catch { // 忽略错误,返回null表示查询失败 } return null; } private void ExportButton_Click(object sender, RoutedEventArgs e) { if (phoneInfos.Count == 0) { MessageBox.Show("没有数据可导出", "提示", MessageBoxButton.OK, MessageBoxImage.Information); return; } var saveFileDialog = new Microsoft.Win32.SaveFileDialog { Filter = "Excel文件|*.xlsx", FileName = $"手机号码归属地_{DateTime.Now:yyyyMMddHHmmss}.xlsx" }; if (saveFileDialog.ShowDialog() == true) { try { // 使用EPPlus导出Excel using (var package = new OfficeOpenXml.ExcelPackage()) { // 创建按省份分类的工作表 var byProvince = package.Workbook.Worksheets.Add("按省份"); byProvince.Cells["A1"].Value = "省份"; byProvince.Cells["B1"].Value = "城市"; byProvince.Cells["C1"].Value = "运营商"; byProvince.Cells["D1"].Value = "手机号码"; var provinceGroups = phoneInfos .Where(p => !string.IsNullOrEmpty(p.Province)) .GroupBy(p => p.Province) .OrderBy(g => g.Key); int row = 2; foreach (var group in provinceGroups) { foreach (var item in group) { byProvince.Cells[row, 1].Value = item.Province; byProvince.Cells[row, 2].Value = item.City; byProvince.Cells[row, 3].Value = item.Operator; byProvince.Cells[row, 4].Value = item.PhoneNumber; row++; } } // 创建按运营商分类的工作表 var byOperator = package.Workbook.Worksheets.Add("按运营商"); byOperator.Cells["A1"].Value = "运营商"; byOperator.Cells["B1"].Value = "省份"; byOperator.Cells["C1"].Value = "城市"; byOperator.Cells["D1"].Value = "手机号码"; var operatorGroups = phoneInfos .Where(p => !string.IsNullOrEmpty(p.Operator)) .GroupBy(p => p.Operator) .OrderBy(g => g.Key); row = 2; foreach (var group in operatorGroups) { foreach (var item in group) { byOperator.Cells[row, 1].Value = item.Operator; byOperator.Cells[row, 2].Value = item.Province; byOperator.Cells[row, 3].Value = item.City; byOperator.Cells[row, 4].Value = item.PhoneNumber; row++; } } // 保存文件 package.SaveAs(new FileInfo(saveFileDialog.FileName)); } MessageBox.Show("导出成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information); txtStatus.Text = $"已导出到 {saveFileDialog.FileName}"; } catch (Exception ex) { MessageBox.Show($"导出失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); txtStatus.Text = "导出失败"; } } } } }

总结与优化

1. 性能优化

  1. ​多线程查询​​:使用Task.Runasync/await实现异步查询,避免UI冻结
  2. ​批量查询​​:对于API查询,可以实现批量查询接口,减少网络请求次数
  3. ​本地缓存​​:将查询结果缓存到本地数据库,减少重复查询

2. 功能扩展

  1. ​号码验证​​:添加手机号码格式验证功能,过滤无效号码
  2. ​历史记录​​:保存查询历史,支持重新加载
  3. ​自定义导出​​:允许用户选择导出字段和排序方式

3. 异常处理

  1. ​网络异常​​:处理API调用失败情况,提供重试机制
  2. ​数据异常​​:处理返回数据格式不正确的情况
  3. ​导出异常​​:处理文件被占用或权限不足的情况

技术要点

  1. ​WPF MVVM模式​​:建议使用MVVM模式重构代码,提高可维护性
  2. ​EPPlus库​​:使用EPPlus库导出Excel,比Interop.Excel更轻量
  3. ​API集成​​:集成第三方归属地查询API,如APISpace
  4. ​本地数据库​​:使用SQLite存储本地号码段数据,提高查询速度
  5.  咕嘎批量手机号码归属地查询系统
  6.  Excel VBA实现批量查询 APISpace手机号码归属地API WPF实现手机号码归属地查询与数据分析
  7. APISpace API调用示例、智能呼叫系统技术实现、WPF导出Excel数据、WPF导入导出Excel

网站公告

今日签到

点亮在社区的每一天
去签到