Exemplo n.º 1
0
        public DataTable BindGrid()
        {
            string    fileName = this.selectDialog.Text;
            DataTable dt       = new DataTable();

            if (!string.IsNullOrEmpty(fileName))
            {
                string   ext           = Path.GetExtension(fileName).ToLower();
                FileType fileType      = FileHelper.GetFileType(ext);
                bool     isFirstColumn = true;
                string[] columns       = new string[] { "联系人", "号码", "号码类型" };
                this.grid1.AutoGenerateColumns = true;
                this.grid1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.grid1.ReadOnly            = true;
                this.grid1.DataSource          = null;
                if (fileType == FileType.Txt)
                {
                    dt = TxtHelper.ParseDataTable(fileName, columns, isFirstColumn);
                }
                else if (fileType == FileType.Excel)
                {
                    ITransferData t = TransferDataFactory.GetTransferData(fileName);
                    dt = t.GetData(fileName, columns, isFirstColumn);
                }
                else
                {
                    dt = TxtHelper.ToDataTable(AddressBook.Parse(fileName), columns, isFirstColumn);
                }
                this.grid1.DataSource = dt;
            }
            return(dt);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 解析通讯录文件
 /// </summary>
 /// <param name="fileName"></param>
 /// <returns></returns>
 public static string Parse(string fileName)
 {
     if (!string.IsNullOrEmpty(fileName))
     {
         string        ext = Path.GetExtension(fileName).ToLower();
         StringBuilder sb  = new StringBuilder();
         try
         {
             string   text     = FileHelper.GetFileContent(fileName, Encoding.UTF8);
             FileType fileType = FileHelper.GetFileType(ext);
             if (!string.IsNullOrEmpty(text))
             {
                 bool     isFirstColumn = false;
                 string[] columns       = new string[] { "联系人", "号码", "号码类型" };
                 string   templ         = "{0}\t{1}\t{2}";
                 if (ext == ".vcf")
                 {
                     sb.AppendLine(string.Format(templ, columns[0], columns[1], columns[2]));
                     string          regexName = @"^(?:FN:)(.*)(?:/*.*)$";
                     MatchCollection matchName = Regex.Matches(text, regexName, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                     string          regexTel  = @"^(?:TEL;TYPE=CELL(?:;TYPE=PREF)?:)((\+86)?\d{11,12})";
                     MatchCollection matchTel  = Regex.Matches(text, regexTel, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                     if (matchName.Count > 0 && matchTel.Count > 0)
                     {
                         for (int i = 0; i < matchName.Count; i++)
                         {
                             string name  = Regex.Replace(matchName[i].Groups[1].Value, "/+.*$", "");
                             string value = matchTel[i].Groups[1].Value;
                             string type  = Regex.IsMatch(value, @"^(\+86){1}\d{11}$") ? "联通" : "移动";
                             string tel   = Regex.Replace(value, @"^(\+86){1}", "");
                             if (tel.StartsWith("0"))
                             {
                                 type = "座机";
                             }
                             sb.AppendLine(string.Format(templ, name, tel, type));
                         }
                     }
                 }
                 else if (fileType == FileType.Txt)
                 {
                     sb.Append(text);
                 }
                 else if (fileType == FileType.Excel)
                 {
                     ITransferData transfer = TransferDataFactory.GetTransferData(fileName);
                     DataTable     dt       = transfer.GetData(fileName, columns, isFirstColumn);
                     sb.Append(TxtHelper.ToTextContent(dt));
                 }
             }
         }
         catch (Exception ex)
         {
             sb.Append(ex.Message);
         }
         return(sb.ToString());
     }
     return(string.Empty);
 }
Exemplo n.º 3
0
        //加载数据
        private void btnLoadImport_Click(object sender, EventArgs e)
        {
            this.gridImport.AutoGenerateColumns = true;
            this.gridImport.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            string fileName = this.selectDialogImport.Text;

            if (!string.IsNullOrWhiteSpace(fileName))
            {
                ITransferData trans = TransferDataFactory.GetTransferData(fileName);
                DataTable     dt    = trans.GetData(fileName, null, true);
                dt.TableName = Path.GetFileNameWithoutExtension(fileName);
                this.gridImport.DataSource = dt;
            }
        }