void uploadFile(object obj) { ImportStudentSelectionWindow window = obj as ImportStudentSelectionWindow; // 上传文件 System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog(); //openFileDialog1.InitialDirectory = "c:\\"; open.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; open.FilterIndex = 1; open.RestoreDirectory = true; open.AddExtension = true; open.CheckFileExists = true; open.CheckPathExists = true; open.ShowHelp = true; if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var excelResult = NPOIClass.ExcelToDataTable(open.FileName, false); if (excelResult.Item1 == null) { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } else { window.DT = excelResult.Item1; window.DialogResult = true; } } }
private void Grid_Drop(object sender, DragEventArgs e) { string[] file = (string[])e.Data.GetData(DataFormats.FileDrop); if (file.Length > 0) { var dropFile = file[0]; System.IO.FileInfo fileInfo = new System.IO.FileInfo(dropFile); if (fileInfo.Extension.Equals(".xls") || fileInfo.Extension.Equals(".xlsx")) { var excelResult = NPOIClass.ExcelToDataTable(dropFile, false); if (excelResult.Item1 == null) { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } else { this.DT = excelResult.Item1; this.DialogResult = true; } } else { this.ShowDialog("提示信息", "支支持xls,xlsx 类型文件!", CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } } }
void Import() { System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog(); open.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; open.FilterIndex = 1; open.RestoreDirectory = true; open.AddExtension = true; open.CheckFileExists = true; open.CheckPathExists = true; open.ShowHelp = true; if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var excelResult = NPOIClass.ExcelToDataTable(open.FileName, true); if (excelResult.Item1 == null) { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } else { var data = excelResult.Item1; var cp = CommonDataManager.GetCPCase(base.LocalID); // 列数 var columnCount = data.Columns.Count; List <string> columns = new List <string>(); foreach (DataColumn col in data.Columns) { columns.Add(col.ColumnName); } // 生成临时集合 var sources = this.values.ToList(); sources.ForEach(v => { v.HasOperation = false; v.IsChecked = false; foreach (var c in v.Courses) { c.Teachers?.Clear(); } }); for (int i = 0; i < data.Rows.Count; i++) { var className = data.Rows[i][0].ToString(); if (!string.IsNullOrEmpty(className)) { var firstClass = sources.FirstOrDefault(c => c.Name.Equals(className)); if (firstClass != null) { var coureses = (from cc in firstClass.Courses from tcc in cp.Courses where cc.CourseID.Equals(tcc.ID) select new { tcc.Name, cc })?.ToList(); coureses?.ForEach(c => { var has = columns.Contains(c.Name); if (has) { c.cc.Teachers = new List <XYKernel.OS.Common.Models.Administrative.TeacherModel>(); var teacherString = data.Rows[i][c.Name]?.ToString(); if (!string.IsNullOrEmpty(teacherString)) { var teachers = teacherString.Split(',')?.ToList(); teachers?.ForEach(t => { var firstTeacher = cp.Teachers.FirstOrDefault(ct => ct.Name.Equals(t)); if (firstTeacher != null) { c.cc.Teachers.Add(firstTeacher); } }); } } }); } } } this.BindData(sources); } } }
void Import() { System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog(); open.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; open.FilterIndex = 1; open.RestoreDirectory = true; open.AddExtension = true; open.CheckFileExists = true; open.CheckPathExists = true; open.ShowHelp = true; if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var excelResult = NPOIClass.ExcelToDataTable(open.FileName, true); if (excelResult.Item1 == null) { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } else { var data = excelResult.Item1; // 列数 var columnCount = data.Columns.Count; List <string> columns = new List <string>(); foreach (DataColumn col in data.Columns) { columns.Add(col.ColumnName); } var hasCourse = columns.Contains("课程"); var hasLevel = columns.Contains("层"); var hasClass = columns.Contains("班级"); var hasTeacher = columns.Contains("教师"); if (!(hasCourse && hasLevel && hasClass && hasTeacher)) { this.ShowDialog("提示信息", "文档格式不正确! 导入失败!"); return; } // 生成临时集合 this.values.ForEach(v => { v.SelectClasses = 0; v.IsChecked = false; foreach (var c in v.Classes) { c.TeacherIDs = new List <string>(); } }); for (int i = 0; i < data.Rows.Count; i++) { var courseName = data.Rows[i]["课程"].ToString(); var levelName = data.Rows[i]["层"].ToString(); var className = data.Rows[i]["班级"].ToString(); var teacherName = data.Rows[i]["教师"].ToString(); if (!string.IsNullOrEmpty(courseName)) { var courseLevel = this.values.FirstOrDefault(v => v.Course.Equals(courseName) && v.Level.Equals(levelName)); if (courseLevel != null) { var firstClass = courseLevel.Classes.FirstOrDefault(c => c.Name.Equals(className)); if (firstClass != null) { if (!string.IsNullOrEmpty(teacherName)) { var teachers = teacherName.Split(','); if (teachers != null) { var teacherIDs = (from t in teachers from st in this.Teachers where t.Equals(st.Name) select st.ID)?.ToList(); if (teacherIDs != null) { firstClass.TeacherIDs?.AddRange(teacherIDs); } } } } } } } this.ImportData(); } } }