private void AddBtn_Click(object sender, RoutedEventArgs e) { string memberName = MemberName.Text.Trim(); string birthday = Birthday.Text.Trim(); string membersex = MenberSex.Text.Trim(); string memberheight = MenberHeight.Text.Trim(); bool flag1 = Regex.IsMatch(memberheight, "^[1-9]\\d*$"); bool flag2 = Regex.IsMatch(memberheight, "^[1-9]\\d*\\.\\d*|0\\.\\d*[1-9]\\d*$"); if (memberName.Length < 1) { popup1.IsOpen = true; } else if (memberName.Length > 5) { popup5.IsOpen = true; } else if (birthday.Length < 1) { popup2.IsOpen = true; } else if (membersex.Length < 1) { popup3.IsOpen = true; } else if (memberheight.Length < 1 || (!flag1 & !flag2)) { popup4.IsOpen = true; } else if (memberheight.Length > 5) { popup4.IsOpen = true; } else { int sex; if (membersex.Equals("男")) { sex = 0; } else { sex = 1; } try { double height = double.Parse(memberheight); bool flag = dbUtill.AddMember(memberName, birthday, sex, height); DialogResult = true; } catch (Exception) { popup4.IsOpen = true; } } }
//批量导入 private void BathInoutBtn_Click(object sender, RoutedEventArgs e) { CheckDataUtill checkDataUtill = new CheckDataUtill(); OpenFileDialog ofd = new OpenFileDialog { Title = "请选择批量导入文件", Filter = "excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*" }; if (ofd.ShowDialog() == DialogResult.OK) { string path = Path.GetFullPath(ofd.FileName); DataTable dt = checkDataUtill.DBExcelToDataTable(path, "Sheet1"); try { for (int i = 0; i < dt.Rows.Count; i++) { string memberName = dt.Rows[i][0].ToString(); string birthday = dt.Rows[i][1].ToString(); string sex = dt.Rows[i][2].ToString(); int sex_int; if (sex.Equals("男")) { sex_int = 0; } else { sex_int = 1; } string height = dt.Rows[i][3].ToString(); double memberHeight = double.Parse(height); dbUtill.AddMember(memberName, birthday, sex_int, memberHeight); } System.Windows.MessageBox.Show("导入成功", "提示"); Init(); } catch (Exception ex) { } } }