private void ImportStudentsFromFile() { OpenFileDialog fileDialog = new OpenFileDialog { Filter = "Excel fileok (*.xls)|*.xls" }; fileDialog.ShowDialog(); if (!String.IsNullOrEmpty(fileDialog.FileName)) { try { XmlDocument doc = new XmlDocument(); doc.Load(fileDialog.FileName); var nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office"); nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel"); nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet"); XmlNodeList nodes = doc.DocumentElement.SelectNodes("//ss:Worksheet/ss:Table/ss:Row", nsmgr); List <Users> importedUsers = new List <Users>(); DataTable dt = new DataTable(); dt.Columns.Add("User", typeof(Users)); dt.Columns.Add("SessionGroup", typeof(string)); dt.Columns.Add("Team", typeof(string)); for (int i = 1; i < nodes.Count; i++) { XmlNodeList dataNodes = nodes[i].SelectNodes("ss:Cell/ss:Data", nsmgr); Users u = new Users { LastName = dataNodes[0].InnerText, FirstName = dataNodes[1].InnerText, NEPTUN = dataNodes[2].InnerText, Email = dataNodes[3].InnerText }; u.FullName = u.LastName + " " + u.FirstName; u.UserName = u.Email; u.UserPassword = UsersDAL.ComputeSha256Hash(u.Email); string sessionGroupName = dataNodes[4].InnerText.Trim(); string teamName = dataNodes[5].InnerText.Trim(); dt.Rows.Add(new object[] { u, sessionGroupName, teamName }); } StudentImportWindow target = new StudentImportWindow(dt) { Owner = this.SourceWindow }; SwitchWindows(target, true); this.StudentList = ReloadStudentList(); } catch (Exception) { MessageBox.Show("Hiba a fájl betöltése közben!", "Hiba", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }
public StudentImportViewModel(DataTable dt, StudentImportWindow source) { this.Table = dt; this.SourceWindow = source; this.SaveCommand = new RelayCommand(ImportStudents); }