void _BWClassSemesterScore_DoWork(object sender, DoWorkEventArgs e) { object[] objectValue = (object[])e.Argument; int schoolyear = (int)objectValue[0]; int semester = (int)objectValue[1]; bool over100 = (bool)objectValue[2]; int papersize = (int)objectValue[3]; bool UseSourceScore = (bool)objectValue[4]; _BWClassSemesterScore.ReportProgress(0); Workbook template = new Workbook(); if (papersize == 0) { template.Open(new MemoryStream(Properties.Resources.班級學生學期成績一覽表B4)); } else if (papersize == 1) { template.Open(new MemoryStream(Properties.Resources.班級學生學期成績一覽表A3)); } Worksheet tempws = template.Worksheets[template.Worksheets.Add()]; // 表頭 0~5 垂直複製 Range tempHeader = template.Worksheets[0].Cells.CreateRange(0, 5, false); // 學業 Range tempEachEntry = template.Worksheets[0].Cells.CreateRange(1, 33, 4, 2); // 學分數 Range tempCreditHeader = template.Worksheets[0].Cells.CreateRange(1, 35, 4, 4); Workbook wb = new Workbook(); wb.Copy(template); Worksheet ws = wb.Worksheets[0]; ScoreAverageMachine machine = new ScoreAverageMachine(); AccessHelper helper = new AccessHelper(); List <StudentRecord> allClassStudent = new List <StudentRecord>(); foreach (ClassRecord aClass in helper.ClassHelper.GetSelectedClass()) { allClassStudent.AddRange(aClass.Students); } double currentStudent = 1; double totalStudent = allClassStudent.Count; int rowIndex = 0; int headerColIndex = 0; foreach (ClassRecord aClass in helper.ClassHelper.GetSelectedClass()) { List <StudentRecord> allStudent = aClass.Students; int headerIndex = rowIndex; int count = 0; if (rowIndex > 0) { ws.Cells.CreateRange(headerIndex - 2, 0, 1, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); ws.Cells.CreateRange(headerIndex - 1, 0, 1, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); } helper.StudentHelper.FillSchoolYearSubjectScore(true, allStudent); helper.StudentHelper.FillSemesterSubjectScore(true, allStudent); helper.StudentHelper.FillSemesterEntryScore(true, allStudent); helper.StudentHelper.FillField("SemesterEntryClassRating", allStudent); //學期分項班排名。 List <string> subjectHeader = new List <string>(); Dictionary <string, string> subjectCreditHeader = new Dictionary <string, string>(); Dictionary <string, string> subjectReqHeader = new Dictionary <string, string>(); List <string> entryHeader = new List <string>(); Dictionary <string, int> columnIndexTable = new Dictionary <string, int>(); foreach (StudentRecord student in allStudent) { foreach (SemesterSubjectScoreInfo info in student.SemesterSubjectScoreList) { // 不計學分、不須評分跳過 || info.Detail.GetAttribute("不需評分") == "是" if (info.Detail.GetAttribute("不計學分") == "是") { continue; } if (info.SchoolYear == schoolyear && info.Semester == semester) { int level; string levelString = ""; if (int.TryParse(info.Level, out level)) { levelString = GetNumber(level); } // //string header = info.Subject + levelString + "_" + (info.Require ? "必 " : "選 ") + "_" + info.Credit; //string creditHeader = (info.Require ? "必 " : "選 ") + info.Credit; string header = info.Subject + levelString + "_" + info.CreditDec(); string creditHeader = info.CreditDec().ToString(); string reqHeader = info.Require ? "必 " : "選 "; if (!subjectHeader.Contains(header)) { subjectHeader.Add(header); subjectCreditHeader.Add(header, creditHeader); subjectReqHeader.Add(header, reqHeader); } } } foreach (SemesterEntryScoreInfo info in student.SemesterEntryScoreList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { string header = info.Entry; // 先過濾原始 // 使用原始成績 if (UseSourceScore && header == "學業(原始)") { header = "學業平均(原始)"; } if (UseSourceScore && header == "學業") { continue; } if (UseSourceScore == false && header == "學業(原始)") { continue; } if (UseSourceScore == false && header == "學業") { header = "學業平均(擇優)"; } if (!entryHeader.Contains(header)) { entryHeader.Add(header); } } } } entryHeader.Add("班級排名"); entryHeader.Sort(SortByEntryName); subjectHeader.Sort(SortBySubjectName); string sString = ""; if (UseSourceScore) { sString = "(原始)"; } else { sString = "(擇優)"; } ws.Cells.CreateRange(rowIndex, 4, false).Copy(tempHeader); ws.Cells[rowIndex, 0].PutValue(SystemInformation.SchoolChineseName + " " + schoolyear + " 學年度 第 " + semester + " 學期 班級學生成績一覽表 班級: " + aClass.ClassName + " " + sString); headerColIndex = 3; foreach (string subject in subjectHeader) { columnIndexTable.Add(subject, headerColIndex); machine.AddItem(subject); string sl = subject.Split('_')[0]; ws.Cells[rowIndex + 1, headerColIndex].PutValue(sl); ws.Cells[rowIndex + 2, headerColIndex].PutValue(subjectReqHeader[subject]); ws.Cells[rowIndex + 3, headerColIndex].PutValue(subjectCreditHeader[subject]); headerColIndex++; } headerColIndex = 33; foreach (string entry in entryHeader) { columnIndexTable.Add(entry, headerColIndex); machine.AddItem(entry); ws.Cells.CreateRange(rowIndex + 1, headerColIndex, 4, 2).Copy(tempEachEntry); ws.Cells[rowIndex + 1, headerColIndex].PutValue(entry); headerColIndex++; } ws.Cells.CreateRange(rowIndex + 1, headerColIndex, 4, 4).Copy(tempCreditHeader); columnIndexTable.Add("應得學分", headerColIndex); columnIndexTable.Add("實得學分", headerColIndex + 1); columnIndexTable.Add("應得學分累計", headerColIndex + 2); columnIndexTable.Add("實得學分累計", headerColIndex + 3); tempws.Cells.CreateRange(0, 1, false).Copy(ws.Cells.CreateRange(rowIndex + 4, 1, false)); Range eachStudent = tempws.Cells.CreateRange(0, 1, false); rowIndex += 4; int defSS = schoolyear * 10 + semester; foreach (StudentRecord student in allStudent) { count++; ws.Cells.CreateRange(rowIndex, 1, false).Copy(eachStudent); ws.Cells[rowIndex, 0].PutValue(student.StudentNumber); ws.Cells[rowIndex, 1].PutValue(student.SeatNo); ws.Cells[rowIndex, 2].PutValue(student.StudentName); decimal shouldGetCredit = 0; decimal gotCredit = 0; decimal shouldGetTotalCredit = 0; decimal gotTotalCredit = 0; foreach (SemesterSubjectScoreInfo info in student.SemesterSubjectScoreList) { // 不計學分、不須評分跳過 || info.Detail.GetAttribute("不需評分") == "是" if (info.Detail.GetAttribute("不計學分") == "是") { continue; } if (info.SchoolYear == schoolyear && info.Semester == semester) { shouldGetCredit += info.CreditDec(); if (info.Pass) { gotCredit += info.CreditDec(); } int level; string levelString = ""; if (int.TryParse(info.Level, out level)) { levelString = GetNumber(level); } //之前 //string key = info.Subject + levelString + "_" + (info.Require ? "必 " : "選 ") + "_" + info.Credit; string key = info.Subject + levelString + "_" + info.CreditDec(); if (columnIndexTable.ContainsKey(key)) { // 判斷使用原始/擇優 decimal iScore = 0; if (UseSourceScore) { decimal.TryParse(info.Detail.GetAttribute("原始成績"), out iScore); } else { iScore = info.Score; // 擇優 } //ws.Cells[rowIndex, columnIndexTable[key]].PutValue((info.Pass ? "" : "*") + info.Score); //machine.AddScore(key, info.Score); ws.Cells[rowIndex, columnIndexTable[key]].PutValue((info.Pass ? "" : "*") + iScore); machine.AddScore(key, iScore); } } // 累計應得、累計實得,調整判斷學年度學期 int iss = info.SchoolYear * 10 + info.Semester; if (iss <= defSS) { shouldGetTotalCredit += info.CreditDec(); if (info.Pass) { gotTotalCredit += info.CreditDec(); } } } foreach (SemesterEntryScoreInfo info in student.SemesterEntryScoreList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { if (UseSourceScore && info.Entry == "學業(原始)") { decimal score = info.Score; if (!over100 && score > 100) { score = 100; } ws.Cells[rowIndex, columnIndexTable["學業平均(原始)"]].PutValue(score); machine.AddScore("學業平均(原始)", score); } else if (UseSourceScore == false && info.Entry == "學業") { decimal score = info.Score; if (!over100 && score > 100) { score = 100; } ws.Cells[rowIndex, columnIndexTable["學業平均(擇優)"]].PutValue(score); machine.AddScore("學業平均(擇優)", score); } else { if (columnIndexTable.ContainsKey(info.Entry)) { decimal score = info.Score; if (!over100 && score > 100) { score = 100; } ws.Cells[rowIndex, columnIndexTable[info.Entry]].PutValue(score); machine.AddScore(info.Entry, score); } } } } ws.Cells[rowIndex, columnIndexTable["應得學分"]].PutValue(shouldGetCredit.ToString()); ws.Cells[rowIndex, columnIndexTable["實得學分"]].PutValue(gotCredit.ToString()); ws.Cells[rowIndex, columnIndexTable["應得學分累計"]].PutValue(shouldGetTotalCredit.ToString()); ws.Cells[rowIndex, columnIndexTable["實得學分累計"]].PutValue(gotTotalCredit.ToString()); SemesterEntryRating rating = new SemesterEntryRating(student); ws.Cells[rowIndex, columnIndexTable["班級排名"]].PutValue(rating.GetPlace(schoolyear, semester)); if (count % 5 == 0) { ws.Cells.CreateRange(rowIndex, 0, 1, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); } rowIndex++; _BWClassSemesterScore.ReportProgress((int)(currentStudent++ *100.0 / totalStudent)); } ws.Cells.CreateRange(headerIndex, 0, 4, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); ws.Cells.CreateRange(rowIndex, 1, false).Copy(eachStudent); //for (int i = 0; i < headerColIndex + 4; i++) //{ // ws.Cells[rowIndex, i].PutValue(""); //} ws.Cells[rowIndex, 0].PutValue("平均"); foreach (string name in machine.GetAllItemName()) { ws.Cells[rowIndex, columnIndexTable[name]].PutValue(machine.GetAverage(name).ToString()); } machine.Clear(); rowIndex++; ws.HPageBreaks.Add(rowIndex, headerColIndex + 4); } ws.Cells.CreateRange(rowIndex - 2, 0, 1, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); ws.Cells.CreateRange(rowIndex - 1, 0, 1, headerColIndex + 4).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); e.Result = wb; }
void _BGWSemesterScoreReport_DoWork(object sender, DoWorkEventArgs e) { object[] objectValue = (object[])e.Argument; int schoolyear = (int)objectValue[0]; int semester = (int)objectValue[1]; Dictionary <string, List <string> > userType = (Dictionary <string, List <string> >)objectValue[2]; MemoryStream template = (MemoryStream)objectValue[3]; int receiver = (int)objectValue[4]; int address = (int)objectValue[5]; string resitSign = (string)objectValue[6]; string repeatSign = (string)objectValue[7]; Entity entity = (Entity)objectValue[8]; bool over100 = (bool)objectValue[9]; _BGWSemesterScoreReport.ReportProgress(0); #region 取得資料 GetPeriodType(); AccessHelper helper = new AccessHelper(); List <StudentRecord> allStudent = new List <StudentRecord>(); if (entity == Entity.Student) { allStudent = helper.StudentHelper.GetSelectedStudent(); FillStudentData(helper, allStudent); } else if (entity == Entity.Class) { foreach (ClassRecord aClass in helper.ClassHelper.GetSelectedClass()) { FillStudentData(helper, aClass.Students); allStudent.AddRange(aClass.Students); } } int currentStudent = 1; int totalStudent = allStudent.Count; //WearyDogComputer computer = new WearyDogComputer(); //computer.FillSemesterSubjectScoreInfoWithResit(helper, true, allStudent); #endregion #region 產生報表並填入資料 Document doc = new Document(); doc.Sections.Clear(); foreach (StudentRecord var in allStudent) { Document eachStudentDoc = new Document(template, "", LoadFormat.Doc, ""); Dictionary <string, object> mergeKeyValue = new Dictionary <string, object>(); #region 學校基本資料 mergeKeyValue.Add("學校名稱", SmartSchool.Customization.Data.SystemInformation.SchoolChineseName); mergeKeyValue.Add("學校地址", SmartSchool.Customization.Data.SystemInformation.Address); mergeKeyValue.Add("學校電話", SmartSchool.Customization.Data.SystemInformation.Telephone); #endregion #region 收件人姓名與地址 if (receiver == 0) { mergeKeyValue.Add("收件人", var.ParentInfo.CustodianName); } else if (receiver == 1) { mergeKeyValue.Add("收件人", var.ParentInfo.FatherName); } else if (receiver == 2) { mergeKeyValue.Add("收件人", var.ParentInfo.MotherName); } else if (receiver == 3) { mergeKeyValue.Add("收件人", var.StudentName); } if (address == 0) { mergeKeyValue.Add("收件人地址", var.ContactInfo.PermanentAddress.FullAddress); } else if (address == 1) { mergeKeyValue.Add("收件人地址", var.ContactInfo.MailingAddress.FullAddress); } #endregion #region 學生基本資料 mergeKeyValue.Add("學年度", schoolyear.ToString()); mergeKeyValue.Add("學期", semester.ToString()); mergeKeyValue.Add("班級科別名稱", (var.RefClass != null) ? var.RefClass.Department : ""); mergeKeyValue.Add("班級", (var.RefClass != null) ? var.RefClass.ClassName : ""); mergeKeyValue.Add("學號", var.StudentNumber); mergeKeyValue.Add("姓名", var.StudentName); mergeKeyValue.Add("座號", var.SeatNo); #endregion #region 導師與評語 if (var.RefClass != null && var.RefClass.RefTeacher != null) { mergeKeyValue.Add("班導師", var.RefClass.RefTeacher.TeacherName); } mergeKeyValue.Add("評語", ""); if (var.SemesterMoralScoreList.Count > 0) { foreach (SemesterMoralScoreInfo info in var.SemesterMoralScoreList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { mergeKeyValue["評語"] = info.SupervisedByComment; } } } #endregion #region 獎懲紀錄 int awardA = 0; int awardB = 0; int awardC = 0; int faultA = 0; int faultB = 0; int faultC = 0; bool ua = false; //留校察看 foreach (RewardInfo info in var.RewardList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { awardA += info.AwardA; awardB += info.AwardB; awardC += info.AwardC; if (!info.Cleared) { faultA += info.FaultA; faultB += info.FaultB; faultC += info.FaultC; } if (info.UltimateAdmonition) { ua = true; } } } mergeKeyValue.Add("大功", awardA.ToString()); mergeKeyValue.Add("小功", awardB.ToString()); mergeKeyValue.Add("嘉獎", awardC.ToString()); mergeKeyValue.Add("大過", faultA.ToString()); mergeKeyValue.Add("小過", faultB.ToString()); mergeKeyValue.Add("警告", faultC.ToString()); if (ua) { mergeKeyValue.Add("留校察看", "ˇ"); } else { mergeKeyValue.Add("留校察看", ""); } #endregion #region 科目成績 Dictionary <SemesterSubjectScoreInfo, Dictionary <string, string> > subjectScore = new Dictionary <SemesterSubjectScoreInfo, Dictionary <string, string> >(); decimal thisSemesterTotalCredit = 0; decimal thisSchoolYearTotalCredit = 0; decimal beforeSemesterTotalCredit = 0; Dictionary <int, decimal> resitStandard = var.Fields["補考標準"] as Dictionary <int, decimal>; foreach (SemesterSubjectScoreInfo info in var.SemesterSubjectScoreList) { string invalidCredit = info.Detail.GetAttribute("不計學分"); string noScoreString = info.Detail.GetAttribute("不需評分"); bool noScore = (noScoreString != "是"); if (invalidCredit == "是") { continue; } if (info.SchoolYear == schoolyear && info.Semester == semester) { if (!subjectScore.ContainsKey(info)) { subjectScore.Add(info, new Dictionary <string, string>()); } subjectScore[info].Add("科目", info.Subject); subjectScore[info].Add("級別", (string.IsNullOrEmpty(info.Level) ? "" : GetNumber(int.Parse(info.Level)))); subjectScore[info].Add("學分", info.CreditDec().ToString()); subjectScore[info].Add("分數", noScore ? info.Score.ToString() : ""); subjectScore[info].Add("必修", ((info.Require) ? "必" : "選")); //判斷補考或重修 if (!info.Pass) { if (info.Score >= resitStandard[info.GradeYear]) { subjectScore[info].Add("補考", "是"); } else { subjectScore[info].Add("補考", "否"); } } } if (info.Pass) { if (info.SchoolYear == schoolyear && info.Semester == semester) { thisSemesterTotalCredit += info.CreditDec(); } if (info.SchoolYear < schoolyear) { beforeSemesterTotalCredit += info.CreditDec(); } else if (info.SchoolYear == schoolyear && info.Semester <= semester) { beforeSemesterTotalCredit += info.CreditDec(); } if (info.SchoolYear == schoolyear) { thisSchoolYearTotalCredit += info.CreditDec(); } } } //if (schoolyearscore) //{ // foreach (SchoolYearSubjectScoreInfo info in var.SchoolYearSubjectScoreList) // { // if (info.SchoolYear == schoolyear) // { // string subject = info.Subject; // foreach (SemesterSubjectScoreInfo key in subjectScore.Keys) // { // if (subjectScore[key]["科目"] == subject && !subjectScore[key].ContainsKey("學年成績")) // subjectScore[key].Add("學年成績", info.Score.ToString()); // } // } // } //} mergeKeyValue.Add("科目成績起始位置", new object[] { subjectScore, resitSign, repeatSign, var }); //mergeKeyValue.Add("取得學分數", "學期" + (schoolyearscore ? "/學年" : "") + "取得學分數"); //mergeKeyValue.Add("名次", ""); //mergeKeyValue.Add("學分數", thisSemesterTotalCredit.ToString()); //mergeKeyValue.Add("累計學分數", beforeSemesterTotalCredit.ToString()); #endregion #region 分項成績 Dictionary <string, Dictionary <string, string> > entryScore = new Dictionary <string, Dictionary <string, string> >(); foreach (SemesterEntryScoreInfo info in var.SemesterEntryScoreList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { string entry = info.Entry; if (!entryScore.ContainsKey(entry)) { entryScore.Add(entry, new Dictionary <string, string>()); } entryScore[entry].Add("分數", info.Score.ToString()); } } //如果是下學期,就多列印學年德行成績。 if (semester == 2) { foreach (SchoolYearEntryScoreInfo info in var.SchoolYearEntryScoreList) { if (info.SchoolYear == schoolyear) { string entry = info.Entry; if (entry == "德行") { entryScore.Add("學年德行成績", new Dictionary <string, string>()); entryScore["學年德行成績"].Add("分數", info.Score.ToString()); } } } } SemesterEntryRating rating = new SemesterEntryRating(var); Dictionary <string, string> totalCredit = new Dictionary <string, string>(); totalCredit.Add("學業成績名次", rating.GetPlace(schoolyear, semester)); totalCredit.Add("學期取得學分數", thisSemesterTotalCredit.ToString()); totalCredit.Add("累計取得學分數", beforeSemesterTotalCredit.ToString()); mergeKeyValue.Add("分項成績起始位置", new object[] { entryScore, totalCredit, over100 }); #endregion #region 缺曠紀錄 Dictionary <string, int> absenceInfo = new Dictionary <string, int>(); foreach (string periodType in userType.Keys) { foreach (string absence in userType[periodType]) { if (!absenceInfo.ContainsKey(periodType + "_" + absence)) { absenceInfo.Add(periodType + "_" + absence, 0); } } } foreach (AttendanceInfo info in var.AttendanceList) { if (info.SchoolYear == schoolyear && info.Semester == semester) { if (PeriodTypeDic.ContainsKey(info.Period)) //2011/1/25 by dylan { if (absenceInfo.ContainsKey(PeriodTypeDic[info.Period] + "_" + info.Absence)) { absenceInfo[PeriodTypeDic[info.Period] + "_" + info.Absence]++; } } } } mergeKeyValue.Add("缺曠紀錄", new object[] { userType, absenceInfo }); #endregion eachStudentDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(MailMerge_MergeField); eachStudentDoc.MailMerge.RemoveEmptyParagraphs = true; List <string> keys = new List <string>(); List <object> values = new List <object>(); foreach (string key in mergeKeyValue.Keys) { keys.Add(key); values.Add(mergeKeyValue[key]); } eachStudentDoc.MailMerge.Execute(keys.ToArray(), values.ToArray()); doc.Sections.Add(doc.ImportNode(eachStudentDoc.Sections[0], true)); //回報進度 _BGWSemesterScoreReport.ReportProgress((int)(currentStudent++ *100.0 / totalStudent)); } #endregion e.Result = doc; }