示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //string output;
            //output = pd.Lookup("14794299760").ToString();
            OpenFileDialog openDlg = new OpenFileDialog();
                        // 指定打开文本文件(后缀名为txt)
                        openDlg.Filter = "excel(*.xls)|*.xls|excel(*.xlsx)|*.xlsx";

            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                dataGridView1.Rows.Clear();
                Loading.Text      = "数据加载中...";
                currCount         = 0;
                noSearchCount     = 0;
                noSearchPhoneList = new List <string>();
                ClearLoadList();
                lv_province.Clear();
                Task.Factory.StartNew((state) =>
                {
                    //excel文件路径
                    string filePath = openDlg.FileName;
                    //获取到工作簿
                    try
                    {
                        var workFile = NPOIExcelHelper.OpenWorkbook(filePath);

                        List <DataGridViewRow> dataGridViewRows = new List <DataGridViewRow>();
                        //获取行信息
                        List <string[]> phoneList = NPOIExcelHelper.ReadLines(workFile.GetSheetAt(0), 0, 0, 0);
                        NPOIExcelHelper.CloseWorkbook(workFile);
                        PhoneData pd = new PhoneData("phone.dat");
                        totalCount   = phoneList.Count(m => !string.IsNullOrEmpty(m[0]) && m[0].StartsWith("1"));

                        phoneList.ForEach(m =>
                        {
                            if (m.Length > 0)
                            {
                                string mobile = m[0];
                                if (!string.IsNullOrEmpty(mobile) && mobile.StartsWith("1"))
                                {
                                    var model           = pd.Lookup(mobile);
                                    string Province     = model.Province; //省
                                    string City         = model.City;     //市
                                    string CardType     = model.CardType; //运营商
                                    DataGridViewRow row = new DataGridViewRow();
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = mobile
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = Province
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = City
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = CardType
                                    });
                                    dataGridViewRows.Add(row);
                                    cacheLoadList.Add(model);
                                    if (string.IsNullOrEmpty(Province))
                                    {
                                        noSearchCount++;
                                        noSearchPhoneList.Add(mobile);
                                    }
                                    else
                                    {
                                        currCount++;
                                    }
                                }
                                else
                                {
                                    DataGridViewRow row = new DataGridViewRow();
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = mobile
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = "错误格式"
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = "未识别"
                                    });
                                    row.Cells.Add(new DataGridViewTextBoxCell()
                                    {
                                        Value = "未识别"
                                    });
                                    dataGridViewRows.Add(row);
                                    cacheLoadList.Add(new PhoneData.PhoneRecord()
                                    {
                                        PhoneNum = mobile,
                                        Province = "错误格式",
                                        City     = "未识别",
                                        CardType = "未识别",
                                    });
                                }
                            }
                        });
                        this.Invoke(new EventHandler(delegate
                        {
                            dataGridView1.Rows.AddRange(dataGridViewRows.ToArray());
                        }));
                        lv_province.Items.Add("全部");
                        lab_datalist.Text = currCount.ToString() + "条";
                        var groups        = cacheLoadList.GroupBy(m => m.Province);
                        foreach (var group in groups)
                        {
                            lv_province.Items.Add(group.Key);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("请关闭打开的Excel再进行导入");
                    }
                }, dataGridView1);

                Task.Run(new Action(delegate()
                {
                    RefreshTxt();
                }));
            }
        }