private void cbGroupNames_SelectedIndexChanged(object sender, EventArgs e) { var groupName = this.cbGroupNames.SelectedItem?.ToString(); if (string.IsNullOrWhiteSpace(groupName)) { return; } var matchId = MatchId; var columnSelected = MatchGroupTableDAL.GetColumnCountSelected(matchId, groupName); if (columnSelected.Length == 0) { // 初始化男子,女子个人团体每组人数 this.cbMenAndWomenEveryGroupNum.SelectedItem = "4"; // 初始化双打每组人数 this.cbDoubleEveryGroupNum.SelectedItem = "3"; this.menDataView.DataSource = null; this.WomenDataView.DataSource = null; this.menmenDataView.DataSource = null; this.womenWomenDataView.DataSource = null; this.menWomenDataView.DataSource = null; UnLockControl(); return; } LockControl(); var menWomenColumn = columnSelected[0]; var doubleColumn = columnSelected[1]; this.cbMenAndWomenEveryGroupNum.SelectedItem = menWomenColumn; this.cbDoubleEveryGroupNum.SelectedItem = doubleColumn; var allSex = MatchGroupTableDAL.GetAllProjectsByGroup(matchId, groupName); // 男,女,男男,女女,男女 foreach (var sexName in allSex) { var columnCount = 0; if (sexName == "男" || sexName == "女") { columnCount = Convert.ToInt32(menWomenColumn); } else if (sexName == "男男" || sexName == "女女" || sexName == "男女") { columnCount = Convert.ToInt32(doubleColumn); } // 获取选择组别、项目下的所有运动员信息 var allAthletes = MatchGroupTableDAL.GetAllAthletesByGroupProject(matchId, groupName, sexName); // 设置分组,同单位的不同列,不同行,只参加个人赛的需要标注 var rowCount = (int)Math.Ceiling(allAthletes.Count / (decimal)columnCount); // 需要的组数 var table = MatchGroupTableDAL.GetEveryGroupTable(matchId, groupName, sexName); DisplayInScreen(rowCount, columnCount, sexName, table); } }
private void lbGenerateRecordCard_Click(object sender, EventArgs e) { var groupName = GroupName; var matchId = MatchId; var matchName = MatchName; var allSex = MatchGroupTableDAL.GetAllProjectsByGroup(matchId, groupName); // 男,女,男男,女女,男女 // 不同性别打印不同的记杆卡,同一性别打印在一个Excle中 var table = MatchGroupTableDAL.GetFinalEveryGroupTable(matchId, groupName, SexName); var gName = SwitchSexName(SexName); var fileName = $"{matchName}-{groupName}-{gName}-记杆卡-{DateTime.Now.ToString("yyyyMMddHHmmss")}"; ExcelHelperMatchGroup.GenerateRecoredCard(fileName, matchId, matchName, groupName, SexName, table); }
public static void GenerateMatchGroup(string fileName, string matchId, string matchName, string groupName) { IWorkbook workbook = null; ISheet sheet; string path; workbook = InitialExcelSheet(fileName, workbook, out sheet, out path); // 设置每列宽度 sheet.SetColumnWidth(0, 3500); sheet.SetColumnWidth(1, 3500); sheet.SetColumnWidth(2, 3500); sheet.SetColumnWidth(3, 3500); sheet.SetColumnWidth(4, 3500); sheet.SetColumnWidth(5, 3500); var row = 0; // 设置文档表头 row = SetTableHeader(workbook, sheet, row, $"竞赛分组表", 450); var allSex = MatchGroupTableDAL.GetAllProjectsByGroup(matchId, groupName); // 男,女,男男,女女,男女 foreach (var sexName in allSex.Distinct()) { var sName = SwitchSexName(sexName); // 设置表名 row = SetTableHeader(workbook, sheet, row, $"{matchName}{sName}分组表"); var table = MatchGroupTableDAL.GetEveryGroupTable(matchId, groupName, sexName); if (table.Count == 0) { continue; } var column = table.Max(x => x.Column) + 1; // 设置表头 row = SetColumnHeader(workbook, sheet, row, column); // 设置每行人数 var maxRow = table.Max(x => x.Row); for (var i = 0; i < maxRow + 1; i++) { var athletesRow = CreateRow(sheet, row); var rowHeaderCell = SetCellValue(athletesRow, 0, $"第 {i + 1} 组"); rowHeaderCell.CellStyle = SetCellStyle(workbook, sheet, row, needBorder: true); var athletes = table.Where(x => x.Row == i).OrderBy(x => x.Column).ToList(); for (var j = 0; j < athletes.Count; j++) { var cell = SetCellValue(athletesRow, j + 1, $"{athletes[j].Name} \n {athletes[j].TeamShortName}"); cell.CellStyle = SetCellStyle(workbook, sheet, row, needBorder: true); if (athletes[j].IsIndividualGroupMatch && (sexName == "男" || sexName == "女")) { SetCellValue(athletesRow, j + 1, $"{athletes[j].Name} \n {athletes[j].TeamShortName} ■"); } } ++row; } } using (FileStream fileStream1 = File.OpenWrite(path)) { workbook.Write(fileStream1); } StringBuilder sb = new StringBuilder(); sb.AppendLine("报表已生成"); sb.AppendLine($"路径:{Path.GetDirectoryName(path)}"); sb.AppendLine($"报表文件:{fileName}.xlsx"); MessageBox.Show(sb.ToString(), "提示"); // 打开生成的报表 System.Diagnostics.Process.Start(path); }
private void lbGenerateMatchGroup_Click(object sender, EventArgs e) { var doubleColumn = this.cbDoubleEveryGroupNum.SelectedItem?.ToString(); var menWomenColumn = this.cbMenAndWomenEveryGroupNum.SelectedItem?.ToString(); var groupNameSelected = this.cbGroupNames.SelectedItem?.ToString(); if (string.IsNullOrWhiteSpace(doubleColumn) || string.IsNullOrWhiteSpace(groupNameSelected) || string.IsNullOrWhiteSpace(menWomenColumn)) { MessageBox.Show("请先定义组别, 男子女子个人团体每组人数,双打每组人数, 再生成竞赛分组表"); return; } var groupName = groupNameSelected; var allSex = MatchGroupTableDAL.GetAllProjectsByGroup(MatchId, groupNameSelected); // 男,女,男男,女女,男女 // 根据不同的sexName生成不同的竞赛分组表 foreach (var sexName in allSex) { var columnCount = 0; if (sexName == "男" || sexName == "女") { columnCount = Convert.ToInt32(menWomenColumn); } else if (sexName == "男男" || sexName == "女女" || sexName == "男女") { columnCount = Convert.ToInt32(doubleColumn); } // 获取选择组别、项目下的所有参加杆数赛的运动员信息 var allAthletes = MatchGroupTableDAL.GetAllAthletesByGroupProject(MatchId, groupName, sexName); if (allAthletes.Count == 0) { continue; } // 设置分组,同单位的不同列,不同行,只参加个人赛的需要标注 var rowCount = (int)Math.Ceiling(allAthletes.Count / (decimal)columnCount); // 需要的组数 var newAthletes = new List <MatchGroupTableAthleteModel>(); allAthletes.ForEach(i => newAthletes.Add(i)); // 排序结果: // 1. 同组人员尽可能分为上下两区 // 2. 同组人员不同行不同列 // 3. 如果人还多,则同列,按照同每列人数从2开始逐渐递增,直至没有多余的列或者最大列数 // 4. 如果人还多便可以同行,同行人数最大每行容纳每组人数两人,否则报错 // 对当前的运动员信息按照每队的人数从大到小进行排序, 如果人数最大的组的数目 > 2 * 组数,提示报错,每组不该超过两人该队人员 // 遍历每一只队伍,进行三轮排序直到该队伍人数排完为止 // 第一轮,不同行,不同列,行 + 列,最小/最大位置切换填充人员 // 第二轮,不同行,同列,行 + 列,最小/最大位置切换填充人员(每列人数从2~总行数) // 第三轮, 不同列同行数不超过2人,行 + 列,最小/最大位置切换填充人员,遇到行 + 列数目相同的单元格,选择列中同队人员最小的一个 // 按队伍人数从多到少的排序 var allSortedTeamNames = GetAllSortedTeamNames(newAthletes, rowCount, sexName); // 初始化一个竞赛分组表 var table = GenerateMatchTable(rowCount, columnCount); // 对每一只队伍进行排序 for (var i = 0; i < allSortedTeamNames.Count; i++) { var willSortedAthletes = newAthletes.Where(x => x.TeamName == allSortedTeamNames[i]).ToList(); SortEveryTeamAthletes(table, willSortedAthletes); } // 展示到界面上 DisplayInScreen(rowCount, columnCount, sexName, table); #region 将生成的分组竞赛表存入到DB中 MatchGroupTableDAL.InsertMatchGroupTableBatch(MatchId, table, sexName, groupName, Convert.ToInt32(menWomenColumn), Convert.ToInt32(doubleColumn)); #endregion } LockControl(); }