void Export() { var local = CommonDataManager.GetLocalCase(base.LocalID); var cp = CommonDataManager.GetCPCase(base.LocalID); System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}(教师批量设置)"; var result = saveDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataColumn classColumn = new System.Data.DataColumn() { ColumnName = "班级" }; dt.Columns.Add(classColumn); cp.Courses.ForEach(c => { var newColumn = new System.Data.DataColumn() { ColumnName = c.Name }; dt.Columns.Add(newColumn); }); this.values.ForEach(v => { System.Data.DataRow newRow = dt.NewRow(); newRow["班级"] = v.Name; if (v.Courses != null) { foreach (var course in v.Courses) { newRow[course.Course] = course.TeacherString; } } dt.Rows.Add(newRow); }); var excelResult = NPOIClass.DataTableToExcel(dt, saveDialog.FileName); if (excelResult.Item1) { this.ShowDialog("提示信息", "导出成功", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); } else { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } } }
void createTemplate() { // 创建模板 var cl = CommonDataManager.GetCLCase(base.LocalID); System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"走班导入学生选课模板.xls"; var result = saveDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { // 获取志愿 //var preselections = (from c in cl.Courses // from cc in c.Levels // select new UIPreselection() // { // CourseID = c.ID, // Course = c.Name, // LevelID = cc.ID, // Level = cc.Name, // })?.ToList(); // 添加列 System.Data.DataTable dt = new System.Data.DataTable(); dt.Columns.Add("姓名", Type.GetType("System.String")); foreach (var c in cl.Courses) { dt.Columns.Add(c.Name, Type.GetType("System.String")); } foreach (var s in cl.Students) { System.Data.DataRow newRow = dt.NewRow(); newRow["姓名"] = s.Name; dt.Rows.Add(newRow); } var excelResult = NPOIClass.DataTableToExcel(dt, saveDialog.FileName); if (excelResult.Item1) { this.ShowDialog("提示信息", "生成模板成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); FileHelper.OpenFilePath(saveDialog.FileName); } else { this.ShowDialog("提示信息", $"生成模板失败-{excelResult.Item2}!", CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); } } }
void Export() { var local = CommonDataManager.GetLocalCase(base.LocalID); var cp = CommonDataManager.GetCPCase(base.LocalID); System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}(教师批量设置)"; var result = saveDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { System.Data.DataTable dt = new System.Data.DataTable(); System.Data.DataColumn courseColumn = new System.Data.DataColumn() { ColumnName = "课程" }; dt.Columns.Add(courseColumn); System.Data.DataColumn levelColumn = new System.Data.DataColumn() { ColumnName = "层" }; dt.Columns.Add(levelColumn); System.Data.DataColumn classColumn = new System.Data.DataColumn() { ColumnName = "班级" }; dt.Columns.Add(classColumn); System.Data.DataColumn teacherColumn = new System.Data.DataColumn() { ColumnName = "教师" }; dt.Columns.Add(teacherColumn); this.values.ForEach(v => { if (v.Classes != null) { foreach (var classInfo in v.Classes) { System.Data.DataRow newRow = dt.NewRow(); newRow["课程"] = v.Course; newRow["层"] = v.Level; newRow["班级"] = classInfo.Name; newRow["教师"] = classInfo.TeacherIDs?.Select(tid => { return(this.Teachers.FirstOrDefault(t => t.ID.Equals(tid))?.Name); })?.Parse(); dt.Rows.Add(newRow); } } }); var excelResult = NPOIClass.DataTableToExcel(dt, saveDialog.FileName); if (excelResult.Item1) { this.ShowDialog("提示信息", "导出成功", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); } else { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } } }
void exportStudentSelectionsCommand() { var local = CommonDataManager.GetLocalCase(base.LocalID); System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}(学生志愿)"; var result = saveDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { #region 获取所有学生志愿 var cl = CommonDataManager.GetCLCase(base.LocalID); var preselections = (from c in cl.Courses from cc in c.Levels select new UIPreselection() { CourseID = c.ID, Course = c.Name, LevelID = cc.ID, Level = cc.Name, })?.ToList(); #endregion string filePath = saveDialog.FileName; System.Data.DataTable dt = new System.Data.DataTable(); #region 学生姓名 System.Data.DataColumn studentColumn = new System.Data.DataColumn() { ColumnName = "姓名" }; dt.Columns.Add(studentColumn); #endregion #region 动态添加其它列 foreach (var c in cl.Courses) { dt.Columns.Add( new System.Data.DataColumn() { ColumnName = c.Name }); } #endregion #region 填充行内容 foreach (var student in this.Students) { System.Data.DataRow newRow = dt.NewRow(); newRow["姓名"] = student.Name; student.Preselections.ForEach(sp => { var ps = preselections.FirstOrDefault(p => p.CourseID.Equals(sp.CourseID) && p.LevelID.Equals(sp.LevelID)); if (ps != null) { if (string.IsNullOrEmpty(ps.Level)) { newRow[ps.Course] = ps.Course; } else { newRow[ps.Course] = ps.Level; } } }); dt.Rows.Add(newRow); } #endregion var excelResult = NPOIClass.DataTableToExcel(dt, filePath); if (excelResult.Item1) { this.ShowDialog("提示信息", "导出成功", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); FileHelper.OpenFilePath(filePath); } else { this.ShowDialog("提示信息", excelResult.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Error); return; } } }
public void exportTable(UIResult result) { var adjustRecord = base.LocalID.DeSerializeAdjustRecord <ResultAdjustmentModel>(result.TaskID); if (adjustRecord != null) { if (!result.IsUploaded) { this.ShowDialog("提示信息", "本地结果发生改变请先上传结果!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } } // (1:班级 2:教师 4 年级) int exportType = 1; ExportTypeWindow window = new ExportTypeWindow(false); window.Closed += (s, arg) => { if (window.DialogResult.Value) { exportType = window.Type; #region 导出 var cp = CommonDataManager.GetCPCase(base.LocalID); var local = CommonDataManager.GetLocalCase(base.LocalID); ResultModel resultModel = base.LocalID.DeSerializeLocalResult <ResultModel>(result.TaskID); if (resultModel == null) { var value = WebAPI.Instance.GetAdminResult(result.TaskID); if (value.Item1) { resultModel = value.Item2; } else { this.ShowDialog("提示信息", "获取行政班结果失败", CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); return; } } string typeName = string.Empty; if (exportType == 1) { typeName = "班级课表"; } else if (exportType == 2) { typeName = "教师课表"; } else if (exportType == 4) { typeName = "年级课表"; } System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}-{result.TaskID}-({typeName})"; var confirm = saveDialog.ShowDialog(); if (confirm == System.Windows.Forms.DialogResult.OK) { if (exportType == 1) { Dictionary <string, DataTable> values = new Dictionary <string, DataTable>(); foreach (var rc in resultModel.ResultClasses) { // 创建基础结构 var dt = this.CreateTableFrame(); // 创建sheet var firstClass = cp.Classes.FirstOrDefault(c => rc.ClassID.Equals(c.ID)); values.Add(firstClass.Name, dt); // 常规 var normals = rc.ResultDetails.Where(rd => rd.ResultType == XYKernel.OS.Common.Enums.ClassHourResultType.Normal)?.ToList(); normals?.ForEach(n => { var classHourInfo = cp.GetClassHours(new int[] { n.ClassHourId })?.FirstOrDefault(); SetCellData(dt, classHourInfo.Course + "\n" + classHourInfo.TeacherString, n.DayPeriod); }); // 单双周 var mulitplys = rc.ResultDetails.Where(rd => rd.ResultType != XYKernel.OS.Common.Enums.ClassHourResultType.Normal)?.ToList(); var groups = mulitplys?.GroupBy(m => $"{m.DayPeriod.Day}{m.DayPeriod.Period}"); if (groups != null) { foreach (var g in groups) { var first = g.FirstOrDefault(); var courseName = g.Select(gi => { return(cp.Courses.FirstOrDefault(c => c.ID.Equals(gi.CourseID))?.Name); })?.Parse("|"); var teacherName = g.Select(gi => { return(cp.GetTeachersByIds(gi.Teachers.ToList()).Select(a => a.Name).ToArray().Parse(",")); })?.Parse("|"); SetCellData(dt, courseName + "\n" + teacherName, first.DayPeriod); } } } #region 导出 List <int> enableIndex = new List <int>(); var abIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.AB); if (abIndex != null) { enableIndex.Add(abIndex.DayPeriod.Period); } var noonIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.Noon); if (noonIndex != null) { enableIndex.Add(noonIndex.DayPeriod.Period); } var pbIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.PB); if (pbIndex != null) { enableIndex.Add(pbIndex.DayPeriod.Period); } var table = NPOIClass.DataTableToExcel(values, saveDialog.FileName, enableIndex); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); FileHelper.OpenFilePath(saveDialog.FileName); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } #endregion } else if (exportType == 4) { GradeExcelModel excelModel = new GradeExcelModel(); // 检查是否有周六周日 List <string> weeks = new List <string>() { "星期一", "星期二", "星期三", "星期四", "星期五", }; // 是否有周末 var hasWeekend = resultModel.ResultClasses.Any(rc => rc.ResultDetails.Any(rd => (rd.DayPeriod.Day == DayOfWeek.Saturday || rd.DayPeriod.Day == DayOfWeek.Sunday))); if (hasWeekend) { weeks.Add("星期六"); weeks.Add("星期日"); } // 节次 List <string> periods = new List <string>(); cp.Positions.GroupBy(p => p.DayPeriod.PeriodName)?.ToList()?.ForEach(p => { periods.Add(p.Key); }); // 设置节次 excelModel.SetPeriods(periods); // 设置星期 excelModel.SetWeeks(weeks); foreach (var rc in resultModel.ResultClasses) { var classInfo = resultModel.Classes.FirstOrDefault(c => rc.ClassID.Equals(c.ID)); // 添加结果 List <string> values = new List <string>(); excelModel.AddClassesDictionary(classInfo.Name, values); var mondays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Monday)?.ToList(); var tuesdays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Tuesday)?.ToList(); var wednesdays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Wednesday)?.ToList(); var thursdays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Thursday)?.ToList(); var fridays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Friday)?.ToList(); var saturdays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Saturday)?.ToList(); var sundays = rc.ResultDetails.Where(rd => rd.DayPeriod.Day == DayOfWeek.Sunday)?.ToList(); values.AddRange(FillGradeExportValues(mondays, periods)); values.AddRange(FillGradeExportValues(tuesdays, periods)); values.AddRange(FillGradeExportValues(wednesdays, periods)); values.AddRange(FillGradeExportValues(thursdays, periods)); values.AddRange(FillGradeExportValues(fridays, periods)); if (saturdays?.Count > 0) { values.AddRange(FillGradeExportValues(saturdays, periods)); } if (sundays?.Count > 0) { values.AddRange(FillGradeExportValues(sundays, periods)); } } var table = GradeExcel.Export(excelModel, saveDialog.FileName); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); FileHelper.OpenFilePath(saveDialog.FileName); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } } else if (exportType == 2) { Dictionary <string, DataTable> values = new Dictionary <string, DataTable>(); // 获取所有教师 var classHourIDs = from c in resultModel.ResultClasses from rd in c.ResultDetails select rd.ClassHourId; var classHours = cp.GetClassHours(classHourIDs?.ToArray()); List <UITeacher> teachers = new List <UITeacher>(); classHours?.ForEach(ch => { ch.Teachers.ForEach(t => { var teacher = teachers.FirstOrDefault(tt => tt.ID.Equals(t.ID)); if (teacher == null) { teachers.Add(new UITeacher() { ID = t.ID, Name = t.Name, ClassHourIDs = new List <int> { ch.ID } }); } else { teacher.ClassHourIDs.Add(ch.ID); } }); }); if (teachers.Count == 0) { this.ShowDialog("提示信息", "没有教师信息无法导出教师课表!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } teachers.ForEach(t => { // 创建基础结构 var dt = this.CreateTableFrame(); values.Add(t.Name, dt); var resultDetails = (from rc in resultModel.ResultClasses from rd in rc.ResultDetails select new { rc.ClassID, rd.ClassHourId, rd.CourseID, rd.DayPeriod, rd.ResultType }); var filters = (from ch in t.ClassHourIDs from rc in resultDetails where ch == rc.ClassHourId select rc)?.ToList(); var groups = filters?.GroupBy(g => $"{g.DayPeriod.Day}{g.DayPeriod.Period}"); if (groups != null) { foreach (var g in groups) { var dayPeriod = g.FirstOrDefault().DayPeriod; var ids = g.Select(gs => { return(gs.ClassHourId); })?.ToArray(); var setValue = cp.GetClassHours(ids)?.Select(ss => $"{ss.Course}-{ss.Class}")?.Parse("|"); SetCellData(dt, setValue, dayPeriod); } } }); #region 导出 List <int> enableIndex = new List <int>(); var abIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.AB); if (abIndex != null) { enableIndex.Add(abIndex.DayPeriod.Period); } var noonIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.Noon); if (noonIndex != null) { enableIndex.Add(noonIndex.DayPeriod.Period); } var pbIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.PB); if (pbIndex != null) { enableIndex.Add(pbIndex.DayPeriod.Period); } var table = NPOIClass.DataTableToExcel(values, saveDialog.FileName, enableIndex); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); FileHelper.OpenFilePath(saveDialog.FileName); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } #endregion } } #endregion } }; window.ShowDialog(); }
public void exportTable(UIResult result) { var adjustRecord = base.LocalID.DeSerializeAdjustRecord <ResultAdjustmentModel>(result.TaskID); if (adjustRecord != null) { if (!result.IsUploaded) { this.ShowDialog("提示信息", "本地结果发生改变上传后才可以导出!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } } // (1:班级 2:教师 3:学生) int exportType = 1; ExportTypeWindow window = new ExportTypeWindow(true); window.Closed += (s, arg) => { if (window.DialogResult.Value) { exportType = window.Type; #region 导出 var cl = CommonDataManager.GetCLCase(base.LocalID); var local = CommonDataManager.GetLocalCase(base.LocalID); ResultModel resultModel = base.LocalID.DeSerializeLocalResult <ResultModel>(result.TaskID); if (resultModel == null) { var value = OSHttpClient.Instance.GetResult(result.TaskID); if (value.Item1) { resultModel = value.Item2; } else { this.ShowDialog("提示信息", "获取走班结果失败", CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); return; } } string typeName = "班级课表"; if (exportType == 2) { typeName = "教师课表"; } else if (exportType == 3) { typeName = "学生课表"; } System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}-{result.TaskID}-({typeName})"; var confirm = saveDialog.ShowDialog(); if (confirm == System.Windows.Forms.DialogResult.OK) { Dictionary <string, DataTable> values = new Dictionary <string, DataTable>(); Dictionary <string, List <UIExcelExport> > studentPreselections = new Dictionary <string, List <UIExcelExport> >(); if (exportType == 1) { var dt = this.CreateTableFrame(); values.Add("课表", dt); var details = (from c in resultModel.ResultClasses from rc in c.ResultDetails select new { c.ClassID, rc.ClassHourId, rc.DayPeriod, rc.Teachers, rc }); var groups = details.GroupBy(g => $"{g.DayPeriod.Day}|{g.DayPeriod.Period}|{g.DayPeriod.PeriodName}")?.ToList(); groups?.ForEach(g => { var first = g.First(); var dayPeriod = first.DayPeriod; StringBuilder sb = new StringBuilder(); g.ToList()?.ForEach(gi => { sb.AppendLine(cl.GetClassByID(gi.ClassID)?.Display); if (gi.Teachers != null) { var teachers = cl.GetTeachersByIds(gi.Teachers.ToList()); sb.AppendLine(teachers.Select(t => t.Name).Parse()); sb.AppendLine(); } }); //var setValue = g.ToList()?.Select(gi => // { // return cl.GetClassByID(gi.ClassID)?.Display; // })?.Parse("\n"); if (sb.Length > 0) { SetCellData(dt, sb.ToString(), dayPeriod); } }); } else if (exportType == 2) { // 获取所有教师 var classHourIDs = from c in resultModel.ResultClasses from rd in c.ResultDetails select rd.ClassHourId; var classHours = cl.GetClassHours(classHourIDs?.ToArray()); List <UITeacher> teachers = new List <UITeacher>(); classHours?.ForEach(ch => { ch.Teachers.ForEach(t => { var teacher = teachers.FirstOrDefault(tt => tt.ID.Equals(t.ID)); if (teacher == null) { teachers.Add(new UITeacher() { ID = t.ID, Name = t.Name, ClassHourIDs = new List <int> { ch.ID } }); } else { teacher.ClassHourIDs.Add(ch.ID); } }); }); if (teachers.Count == 0) { this.ShowDialog("提示信息", "没有教师信息无法导出教师课表!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } teachers.ForEach(t => { // 创建基础结构 var dt = this.CreateTableFrame(); values.Add(t.Name, dt); var resultDetails = (from rc in resultModel.ResultClasses from rd in rc.ResultDetails select new { rc.ClassID, rd.ClassHourId, rd.DayPeriod, }); var filters = (from ch in t.ClassHourIDs from rc in resultDetails where ch == rc.ClassHourId select rc)?.ToList(); var groups = filters?.GroupBy(g => $"{g.DayPeriod.Day}|{g.DayPeriod.Period}|{g.DayPeriod.PeriodName}")?.ToList(); groups?.ForEach(g => { var first = g.First(); var dayPeriod = first.DayPeriod; var setValue = g.ToList()?.Select(gi => { return(cl.GetClassByID(gi.ClassID)?.Display); })?.Parse("\n"); SetCellData(dt, setValue, dayPeriod); }); }); } else if (exportType == 3) { var preselectionsGroups = (from stu in cl.Students from sp in stu.Preselections where stu.Preselections?.Count > 0 select new { stu.ID, stu.Name, stu.Preselections, groupString = stu.Preselections.OrderBy(o => o.CourseID).OrderBy(o => o.LevelID).Select(pss => $"{pss.CourseID}|{pss.LevelID}")?.Parse() })?.GroupBy(g => g.groupString)?.ToList(); preselectionsGroups?.ForEach(g => { var first = g.First(); var sheetString = first.Preselections.Select(p => { var courseInfo = cl.Courses.FirstOrDefault(c => c.ID.Equals(p.CourseID)); if (courseInfo != null) { var levelInfo = courseInfo.Levels.FirstOrDefault(l => l.ID.Equals(p.LevelID)); if (levelInfo != null) { return($"{courseInfo.Name}{levelInfo.Name}"); } else { return($"{courseInfo.Name}"); } } else { return(string.Empty); } })?.Parse(); List <UIExcelExport> exports = new List <UIExcelExport>(); var students = g.Distinct()?.ToList(); students.ForEach(gi => { // 创建表格 var dt = this.CreateTableFrame(); UIExcelExport export = new UIExcelExport(); export.Table = dt; export.CreateTime = DateTime.Now; export.StudentID = gi.ID; export.Student = gi.Name; // 获取结果数据 var resultDetails = (from rc in resultModel.ResultClasses from rd in rc.ResultDetails where rc.ResultStudents.Contains(gi.ID) select new { rc.ClassID, rd.ClassHourId, rd.DayPeriod, }); // 根据位置分组 var groups = resultDetails?.Where(rd => { var classHour = cl.GetClassHours(new int[] { rd.ClassHourId })?.FirstOrDefault(); if (classHour == null) { return(false); } else { if (gi.Preselections == null) { return(false); } else { return(gi.Preselections.Any(p => p.CourseID.Equals(classHour.CourseID) && p.LevelID.Equals(classHour.LevelID))); } } })?.GroupBy(gg => $"{gg.DayPeriod.Day}{gg.DayPeriod.Period}")?.ToList(); // 填充表格数据 groups?.ForEach(gg => { var firstgg = gg.First(); var dayPeriod = firstgg.DayPeriod; StringBuilder sb = new StringBuilder(); gg.ToList()?.ForEach(ggi => { sb.AppendLine(cl.GetClassByID(ggi.ClassID)?.Display); var classInfo = cl.GetClassByID(ggi.ClassID); if (classInfo != null) { if (classInfo.TeacherIDs?.Count > 0) { var teachers = cl.GetTeachersByIds(classInfo.TeacherIDs); var teacherString = teachers?.Select(t => t.Name)?.Parse(); sb.AppendLine(teacherString); sb.AppendLine(); } } }); //var setValue = gg.ToList()?.Select(ggi => //{ // return cl.GetClassByID(ggi.ClassID)?.Display; //})?.Parse("\n"); if (sb.Length > 0) { SetCellData(dt, sb.ToString(), dayPeriod); } }); // 向集合中添加 exports.Add(export); }); studentPreselections.Add(sheetString, exports); }); } List <int> enableIndex = new List <int>(); var abIndex = cl.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.AB); if (abIndex != null) { enableIndex.Add(abIndex.DayPeriod.Period); } var noonIndex = cl.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.Noon); if (noonIndex != null) { enableIndex.Add(noonIndex.DayPeriod.Period); } var pbIndex = cl.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.PB); if (pbIndex != null) { enableIndex.Add(pbIndex.DayPeriod.Period); } if (exportType == 3) { var table = NPOIClass.DataTableToExcel(studentPreselections, saveDialog.FileName, enableIndex); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } } else { var table = NPOIClass.DataTableToExcel(values, saveDialog.FileName, enableIndex); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } } } #endregion } }; window.ShowDialog(); }
public void exportTable(UIResult result) { var adjustRecord = base.LocalID.DeSerializeAdjustRecord <ResultAdjustmentModel>(result.TaskID); if (adjustRecord != null) { if (!result.IsUploaded) { this.ShowDialog("提示信息", "本地结果发生改变请先上传结果!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } } // (1:班级 2:教师) int exportType = 1; ExportTypeWindow window = new ExportTypeWindow(false); window.Closed += (s, arg) => { if (window.DialogResult.Value) { exportType = window.Type; #region 导出 var cp = CommonDataManager.GetCPCase(base.LocalID); var local = CommonDataManager.GetLocalCase(base.LocalID); ResultModel resultModel = base.LocalID.DeSerializeLocalResult <ResultModel>(result.TaskID); if (resultModel == null) { var value = OSHttpClient.Instance.GetAdminResult(result.TaskID); if (value.Item1) { resultModel = value.Item2; } else { this.ShowDialog("提示信息", "获取行政班结果失败", CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); return; } } string typeName = exportType == 1 ? "班级课表" : "教师课表"; System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog(); saveDialog.Filter = "Microsoft Excel files(*.xls)|*.xls;*.xlsx"; saveDialog.FileName = $"{local.Name}-{result.TaskID}-({typeName})"; var confirm = saveDialog.ShowDialog(); if (confirm == System.Windows.Forms.DialogResult.OK) { Dictionary <string, DataTable> values = new Dictionary <string, DataTable>(); if (exportType == 1) { foreach (var rc in resultModel.ResultClasses) { // 创建基础结构 var dt = this.CreateTableFrame(); // 创建sheet var firstClass = cp.Classes.FirstOrDefault(c => rc.ClassID.Equals(c.ID)); values.Add(firstClass.Name, dt); // 常规 var normals = rc.ResultDetails.Where(rd => rd.ResultType == XYKernel.OS.Common.Enums.ClassHourResultType.Normal)?.ToList(); normals?.ForEach(n => { var classHourInfo = cp.GetClassHours(new int[] { n.ClassHourId })?.FirstOrDefault(); SetCellData(dt, classHourInfo.Course + "\n" + classHourInfo.TeacherString, n.DayPeriod); }); // 单双周 var mulitplys = rc.ResultDetails.Where(rd => rd.ResultType != XYKernel.OS.Common.Enums.ClassHourResultType.Normal)?.ToList(); var groups = mulitplys?.GroupBy(m => $"{m.DayPeriod.Day}{m.DayPeriod.Period}"); if (groups != null) { foreach (var g in groups) { var first = g.FirstOrDefault(); var courseName = g.Select(gi => { return(cp.Courses.FirstOrDefault(c => c.ID.Equals(gi.CourseID))?.Name); })?.Parse("|"); var teacherName = g.Select(gi => { return(cp.GetTeachersByIds(gi.Teachers.ToList()).Select(a => a.Name).ToArray().Parse(",")); })?.Parse("|"); SetCellData(dt, courseName + "\n" + teacherName, first.DayPeriod); } } } } else if (exportType == 2) { // 获取所有教师 var classHourIDs = from c in resultModel.ResultClasses from rd in c.ResultDetails select rd.ClassHourId; var classHours = cp.GetClassHours(classHourIDs?.ToArray()); List <UITeacher> teachers = new List <UITeacher>(); classHours?.ForEach(ch => { ch.Teachers.ForEach(t => { var teacher = teachers.FirstOrDefault(tt => tt.ID.Equals(t.ID)); if (teacher == null) { teachers.Add(new UITeacher() { ID = t.ID, Name = t.Name, ClassHourIDs = new List <int> { ch.ID } }); } else { teacher.ClassHourIDs.Add(ch.ID); } }); }); if (teachers.Count == 0) { this.ShowDialog("提示信息", "没有教师信息无法导出教师课表!", DialogSettingType.OnlyOkButton, DialogType.Warning); return; } teachers.ForEach(t => { // 创建基础结构 var dt = this.CreateTableFrame(); values.Add(t.Name, dt); var resultDetails = (from rc in resultModel.ResultClasses from rd in rc.ResultDetails select new { rc.ClassID, rd.ClassHourId, rd.CourseID, rd.DayPeriod, rd.ResultType }); var filters = (from ch in t.ClassHourIDs from rc in resultDetails where ch == rc.ClassHourId select rc)?.ToList(); var groups = filters?.GroupBy(g => $"{g.DayPeriod.Day}{g.DayPeriod.Period}"); if (groups != null) { foreach (var g in groups) { var dayPeriod = g.FirstOrDefault().DayPeriod; var ids = g.Select(gs => { return(gs.ClassHourId); })?.ToArray(); var setValue = cp.GetClassHours(ids)?.Select(ss => $"{ss.Course}-{ss.Class}")?.Parse("|"); SetCellData(dt, setValue, dayPeriod); } } }); } List <int> enableIndex = new List <int>(); var abIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.AB); if (abIndex != null) { enableIndex.Add(abIndex.DayPeriod.Period); } var noonIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.Noon); if (noonIndex != null) { enableIndex.Add(noonIndex.DayPeriod.Period); } var pbIndex = cp.Positions.FirstOrDefault(p => p.Position == XYKernel.OS.Common.Enums.Position.PB); if (pbIndex != null) { enableIndex.Add(pbIndex.DayPeriod.Period); } var table = NPOIClass.DataTableToExcel(values, saveDialog.FileName, enableIndex); if (table.Item1) { this.ShowDialog("提示信息", "导出成功!", CustomControl.Enums.DialogSettingType.NoButton, CustomControl.Enums.DialogType.None); } else { this.ShowDialog("提示信息", table.Item2, CustomControl.Enums.DialogSettingType.OnlyOkButton, CustomControl.Enums.DialogType.Warning); } } #endregion } }; window.ShowDialog(); }