/// <summary> /// 未達畢業標準通知單 /// </summary> private void ExportDoc() { if (_students.Count == 0) { return; } _doc.Sections.Clear(); if (_rc.Template == null) { _rc.Template = new ReportTemplate(Properties.Resources.未達畢業標準通知單樣板, TemplateType.Word); } _template = _rc.Template.ToDocument(); string UserSelAddresseeAddress = _rc.GetString(PrintConfigForm.setupAddresseeAddress, "聯絡地址"); string UserSelAddresseeName = _rc.GetString(PrintConfigForm.setupAddresseeName, "監護人"); _UserSelExportStudentList = _rc.GetBoolean(PrintConfigForm.setupExportStudentList, false); string UserSeldtDate = ""; DateTime dt; if (DateTime.TryParse(_rc.GetString(PrintConfigForm.setupdtDocDate, ""), out dt)) { UserSeldtDate = ConvertDate1(dt); } else { UserSeldtDate = ConvertDate1(DateTime.Now); } List <StudentGraduationPredictData> StudentGraduationPredictDataList = new List <StudentGraduationPredictData>(); // 取得學生ID,製作 Dict 用 List <string> StudIDList = (from data in _students select data.ID).ToList(); // Student Address,Key:StudentID Dictionary <string, JHAddressRecord> AddressDict = new Dictionary <string, JHAddressRecord>(); // Student Parent,Key:StudentID Dictionary <string, JHParentRecord> ParentDict = new Dictionary <string, JHParentRecord>(); // 地址 foreach (JHAddressRecord rec in JHAddress.SelectByStudentIDs(StudIDList)) { if (!AddressDict.ContainsKey(rec.RefStudentID)) { AddressDict.Add(rec.RefStudentID, rec); } } // 父母監護人 foreach (JHParentRecord rec in JHParent.SelectByStudentIDs(StudIDList)) { if (!ParentDict.ContainsKey(rec.RefStudentID)) { ParentDict.Add(rec.RefStudentID, rec); } } // 資料轉換 .. foreach (StudentRecord StudRec in _students) { if (!_result.ContainsKey(StudRec.ID)) { continue; } //處理年級為0的資料 List <ResultDetail> zeroGrades = new List <ResultDetail>(); StudentGraduationPredictData sgpd = new StudentGraduationPredictData(); if (StudRec.Class != null) { sgpd.ClassName = StudRec.Class.Name; } sgpd.Name = StudRec.Name; sgpd.SchoolAddress = K12.Data.School.Address; sgpd.SchoolName = K12.Data.School.ChineseName; sgpd.SchoolPhone = K12.Data.School.Telephone; sgpd.SeatNo = StudRec.SeatNo; sgpd.StudentNumber = StudRec.StudentNumber; // 文字 if (_result.ContainsKey(StudRec.ID)) { int GrYear; foreach (ResultDetail rd in _result[StudRec.ID]) { if (int.TryParse(rd.GradeYear, out GrYear)) { //if (GrYear == 0) continue; //後續處理 if (GrYear == 0) { zeroGrades.Add(rd); } // 組訊息 string Detail = ""; if (rd.Details.Count > 0) { Detail = string.Join(",", rd.Details.ToArray()); } // 一年級 if (GrYear == 1 || GrYear == 7) { if (rd.Semester.Trim() == "1") { sgpd.Text11 = Detail; } if (rd.Semester.Trim() == "2") { sgpd.Text12 = Detail; } } // 二年級 if (GrYear == 2 || GrYear == 8) { if (rd.Semester.Trim() == "1") { sgpd.Text21 = Detail; } if (rd.Semester.Trim() == "2") { sgpd.Text22 = Detail; } } // 三年級 if (GrYear == 3 || GrYear == 9) { if (rd.Semester.Trim() == "1") { sgpd.Text31 = Detail; } if (rd.Semester.Trim() == "2") { sgpd.Text32 = Detail; } } } } } // 地址 if (AddressDict.ContainsKey(StudRec.ID)) { if (UserSelAddresseeAddress == "聯絡地址") { sgpd.AddresseeAddress = AddressDict[StudRec.ID].MailingAddress; } if (UserSelAddresseeAddress == "戶籍地址") { sgpd.AddresseeAddress = AddressDict[StudRec.ID].PermanentAddress; } } // 父母監護人 if (ParentDict.ContainsKey(StudRec.ID)) { if (UserSelAddresseeName == "父親") { sgpd.AddresseeName = ParentDict[StudRec.ID].FatherName; } if (UserSelAddresseeName == "母親") { sgpd.AddresseeName = ParentDict[StudRec.ID].MotherName; } if (UserSelAddresseeName == "監護人") { sgpd.AddresseeName = ParentDict[StudRec.ID].CustodianName; } } sgpd.DocDate = UserSeldtDate; foreach (ResultDetail rd in zeroGrades) { sgpd.Text += string.Join(",", rd.Details); } StudentGraduationPredictDataList.Add(sgpd); } // 產生Word 套印 Dictionary <string, object> FieldData = new Dictionary <string, object>(); // 班座排序 StudentGraduationPredictDataList = (from data in StudentGraduationPredictDataList orderby data.ClassName, data.SeatNo.PadLeft(3, '0') ascending select data).ToList(); foreach (StudentGraduationPredictData sgpd in StudentGraduationPredictDataList) { FieldData.Clear(); FieldData.Add("學校名稱", sgpd.SchoolName); FieldData.Add("學校電話", sgpd.SchoolPhone); FieldData.Add("學校地址", sgpd.SchoolAddress); FieldData.Add("收件人地址", sgpd.AddresseeAddress); FieldData.Add("收件人姓名", sgpd.AddresseeName); FieldData.Add("班級", sgpd.ClassName); FieldData.Add("座號", sgpd.SeatNo); FieldData.Add("姓名", sgpd.Name); FieldData.Add("學號", sgpd.StudentNumber); FieldData.Add("一上文字", sgpd.Text11); FieldData.Add("一下文字", sgpd.Text12); FieldData.Add("二上文字", sgpd.Text21); FieldData.Add("二下文字", sgpd.Text22); FieldData.Add("三上文字", sgpd.Text31); FieldData.Add("三下文字", sgpd.Text32); FieldData.Add("發文日期", sgpd.DocDate); FieldData.Add("所有說明", sgpd.Text); Aspose.Words.Document each = (Aspose.Words.Document)_template.Clone(true); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(each); // 合併 if (FieldData.Count > 0) { builder.Document.MailMerge.Execute(FieldData.Keys.ToArray(), FieldData.Values.ToArray()); } foreach (Aspose.Words.Section sec in each.Sections) { _doc.Sections.Add(_doc.ImportNode(sec, true)); } } // 產生學生清單 if (_UserSelExportStudentList) { _wbStudentList = new Workbook(); _wbStudentList.Worksheets[0].Cells[0, 0].PutValue("班級"); _wbStudentList.Worksheets[0].Cells[0, 1].PutValue("座號"); _wbStudentList.Worksheets[0].Cells[0, 2].PutValue("學號"); _wbStudentList.Worksheets[0].Cells[0, 3].PutValue("學生姓名"); _wbStudentList.Worksheets[0].Cells[0, 4].PutValue("收件人姓名"); _wbStudentList.Worksheets[0].Cells[0, 5].PutValue("地址"); //班級 座號 學號 學生姓名 收件人姓名 地址 int rowIdx = 1; foreach (StudentGraduationPredictData sgpd in StudentGraduationPredictDataList) { _wbStudentList.Worksheets[0].Cells[rowIdx, 0].PutValue(sgpd.ClassName); _wbStudentList.Worksheets[0].Cells[rowIdx, 1].PutValue(sgpd.SeatNo); _wbStudentList.Worksheets[0].Cells[rowIdx, 2].PutValue(sgpd.StudentNumber); _wbStudentList.Worksheets[0].Cells[rowIdx, 3].PutValue(sgpd.Name); _wbStudentList.Worksheets[0].Cells[rowIdx, 4].PutValue(sgpd.AddresseeName); _wbStudentList.Worksheets[0].Cells[rowIdx, 5].PutValue(sgpd.AddresseeAddress); rowIdx++; } _wbStudentList.Worksheets[0].AutoFitColumns(); } }
private void _BGWDisciplineNotification_DoWork(object sender, DoWorkEventArgs e) { #region Report if (entityName.ToLower() == "student") //學生模式 { SelectedStudents = K12.Data.Student.SelectByIDs(K12.Presentation.NLDPanels.Student.SelectedSource); } else if (entityName.ToLower() == "class") //班級模式 { SelectedStudents = new List <StudentRecord>(); foreach (StudentRecord each in Student.SelectByClassIDs(K12.Presentation.NLDPanels.Class.SelectedSource)) { if (each.Status != StudentRecord.StudentStatus.一般) { continue; } SelectedStudents.Add(each); } } else { throw new NotImplementedException(); } SelectedStudents.Sort(new Comparison <StudentRecord>(CommonMethods.ClassSeatNoComparer)); #endregion //超級資訊物件 Dictionary <string, StudentOBJ> StudentSuperOBJ = new Dictionary <string, StudentOBJ>(); //所有學生ID List <string> allStudentID = new List <string>(); //學生人數 int currentStudentCount = 1; int totalStudentNumber = 0; #region 依據 ClassID 建立班級學生清單 //List<StudentRecord> classStudent = SelectedStudents; //加總用 Dictionary <string, int> StudMeritSum = new Dictionary <string, int>(); foreach (StudentRecord aStudent in SelectedStudents) { //string aStudentID = aStudent.ID; if (!StudentSuperOBJ.ContainsKey(aStudent.ID)) { StudentSuperOBJ.Add(aStudent.ID, new StudentOBJ()); } //學生ID清單 if (!allStudentID.Contains(aStudent.ID)) { allStudentID.Add(aStudent.ID); } StudentSuperOBJ[aStudent.ID].student = aStudent; StudentSuperOBJ[aStudent.ID].TeacherName = aStudent.Class != null ? (aStudent.Class.Teacher != null ? aStudent.Class.Teacher.Name : "") : ""; StudentSuperOBJ[aStudent.ID].ClassName = aStudent.Class != null ? aStudent.Class.Name : ""; StudentSuperOBJ[aStudent.ID].SeatNo = aStudent.SeatNo.HasValue ? aStudent.SeatNo.Value.ToString() : ""; StudentSuperOBJ[aStudent.ID].StudentNumber = aStudent.StudentNumber; } #endregion #region 取得獎懲資料(日期區間) List <DemeritRecord> DemeritList = new List <DemeritRecord>(); if (obj.IsInsertDate) //發生日期 { DemeritList = Demerit.SelectByOccurDate(allStudentID, obj.StartDate, obj.EndDate); } else //登錄入期 { DemeritList = Demerit.SelectByRegisterDate(allStudentID, obj.StartDate, obj.EndDate); } string reportName = "留察通知單(" + obj.StartDate.ToString("yyyy-MM-dd") + "至" + obj.EndDate.ToString("yyyy-MM-dd") + ")"; //依日期排序 DemeritList.Sort(SortDateTime); foreach (DemeritRecord var in DemeritList) { string occurMonthDay = var.OccurDate.Month + "/" + var.OccurDate.Day; string reason = var.Reason; if (var.MeritFlag == "2") //1是獎勵 { #region 留察 StringBuilder detailString = new StringBuilder(); detailString.Append(occurMonthDay + " "); //日期 if (!string.IsNullOrEmpty(reason)) { detailString.Append(reason + " "); //事由 } detailString.Append("(留校察看) "); //明細資料 StudentSuperOBJ[var.RefStudentID].DemeritStringList.Add(detailString.ToString()); #endregion } } #endregion #region 取得學生通訊地址資料 foreach (AddressRecord record in Address.SelectByStudentIDs(allStudentID)) { if (obj.ReceiveAddress == "戶籍地址") { if (!string.IsNullOrEmpty(record.PermanentAddress)) { StudentSuperOBJ[record.RefStudentID].address = record.Permanent.County + record.Permanent.Town + record.Permanent.District + record.Permanent.Area + record.Permanent.Detail; } if (!string.IsNullOrEmpty(record.PermanentZipCode)) { StudentSuperOBJ[record.RefStudentID].ZipCode = record.PermanentZipCode; if (record.PermanentZipCode.Length >= 1) { StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.PermanentZipCode.Substring(0, 1); } if (record.PermanentZipCode.Length >= 2) { StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.PermanentZipCode.Substring(1, 1); } if (record.PermanentZipCode.Length >= 3) { StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.PermanentZipCode.Substring(2, 1); } if (record.PermanentZipCode.Length >= 4) { StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.PermanentZipCode.Substring(3, 1); } if (record.PermanentZipCode.Length >= 5) { StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.PermanentZipCode.Substring(4, 1); } } } else if (obj.ReceiveAddress == "聯絡地址") { if (!string.IsNullOrEmpty(record.MailingAddress)) { StudentSuperOBJ[record.RefStudentID].address = record.Mailing.County + record.Mailing.Town + record.Mailing.District + record.Mailing.Area + record.Mailing.Detail; //再處理 } if (!string.IsNullOrEmpty(record.MailingZipCode)) { StudentSuperOBJ[record.RefStudentID].ZipCode = record.MailingZipCode; if (record.MailingZipCode.Length >= 1) { StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.MailingZipCode.Substring(0, 1); } if (record.MailingZipCode.Length >= 2) { StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.MailingZipCode.Substring(1, 1); } if (record.MailingZipCode.Length >= 3) { StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.MailingZipCode.Substring(2, 1); } if (record.MailingZipCode.Length >= 4) { StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.MailingZipCode.Substring(3, 1); } if (record.MailingZipCode.Length >= 5) { StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.MailingZipCode.Substring(4, 1); } } } else if (obj.ReceiveAddress == "其他地址") { if (!string.IsNullOrEmpty(record.Address1Address)) { StudentSuperOBJ[record.RefStudentID].address = record.Address1.County + record.Address1.Town + record.Address1.District + record.Address1.Area + record.Address1.Detail; //再處理 } if (!string.IsNullOrEmpty(record.Address1ZipCode)) { StudentSuperOBJ[record.RefStudentID].ZipCode = record.Address1ZipCode; if (record.Address1ZipCode.Length >= 1) { StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.Address1ZipCode.Substring(0, 1); } if (record.Address1ZipCode.Length >= 2) { StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.Address1ZipCode.Substring(1, 1); } if (record.Address1ZipCode.Length >= 3) { StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.Address1ZipCode.Substring(2, 1); } if (record.Address1ZipCode.Length >= 4) { StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.Address1ZipCode.Substring(3, 1); } if (record.Address1ZipCode.Length >= 5) { StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.Address1ZipCode.Substring(4, 1); } } } } #endregion #region 取得學生監護人父母親資料 List <ParentRecord> ParentList = Parent.SelectByStudentIDs(allStudentID); foreach (ParentRecord record in ParentList) { StudentSuperOBJ[record.RefStudentID].CustodianName = record.CustodianName; StudentSuperOBJ[record.RefStudentID].FatherName = record.FatherName; StudentSuperOBJ[record.RefStudentID].MotherName = record.MotherName; } #endregion #region 產生報表 Aspose.Words.Document template = new Aspose.Words.Document(obj.Template); template.MailMerge.Execute( new string[] { "學校名稱", "學校地址", "學校電話" }, new object[] { School.ChineseName, School.Address, School.Telephone } ); Aspose.Words.Document doc = new Aspose.Words.Document(); doc.RemoveAllChildren(); Aspose.Words.Node sectionNode = template.Sections[0].Clone(); //取得學生人數 totalStudentNumber = StudentSuperOBJ.Count; foreach (string student in StudentSuperOBJ.Keys) { //如果沒有學生就離開 if (obj.PrintHasRecordOnly) { if (StudentSuperOBJ.Count == 0) { continue; } } if (StudentSuperOBJ[student].DemeritStringList.Count == 0) { continue; } Aspose.Words.Document eachDoc = new Aspose.Words.Document(); eachDoc.RemoveAllChildren(); eachDoc.Sections.Add(eachDoc.ImportNode(sectionNode, true)); //合併列印的資料 Dictionary <string, object> mapping = new Dictionary <string, object>(); StudentOBJ eachStudentInfo = StudentSuperOBJ[student]; //學生資料 mapping.Add("系統編號", "系統編號{" + eachStudentInfo.student.ID + "}"); mapping.Add("學生姓名", eachStudentInfo.student.Name); mapping.Add("班級", eachStudentInfo.ClassName); mapping.Add("座號", eachStudentInfo.SeatNo); mapping.Add("學號", eachStudentInfo.StudentNumber); mapping.Add("導師", eachStudentInfo.TeacherName); mapping.Add("資料期間", obj.StartDate.ToShortDateString() + " 至 " + obj.EndDate.ToShortDateString()); //收件人資料 if (obj.ReceiveName == "監護人姓名") { mapping.Add("收件人姓名", eachStudentInfo.CustodianName); } else if (obj.ReceiveName == "父親姓名") { mapping.Add("收件人姓名", eachStudentInfo.FatherName); } else if (obj.ReceiveName == "母親姓名") { mapping.Add("收件人姓名", eachStudentInfo.MotherName); } else { mapping.Add("收件人姓名", eachStudentInfo.student.Name); } //收件人地址資料 mapping.Add("收件人地址", eachStudentInfo.address); mapping.Add("郵遞區號", eachStudentInfo.ZipCode); mapping.Add("0", eachStudentInfo.ZipCode1); mapping.Add("1", eachStudentInfo.ZipCode2); mapping.Add("2", eachStudentInfo.ZipCode3); mapping.Add("4", eachStudentInfo.ZipCode4); mapping.Add("5", eachStudentInfo.ZipCode5); mapping.Add("學年度", School.DefaultSchoolYear); mapping.Add("學期", School.DefaultSemester); //懲戒明細 object[] objectValues = new object[] { StudentSuperOBJ[student].DemeritStringList }; mapping.Add("懲戒明細", objectValues); string[] keys = new string[mapping.Count]; object[] values = new object[mapping.Count]; int i = 0; foreach (string key in mapping.Keys) { keys[i] = key; values[i++] = mapping[key]; } //合併列印 eachDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(DisciplineNotification_MailMerge_MergeField); eachDoc.MailMerge.RemoveEmptyParagraphs = true; eachDoc.MailMerge.Execute(keys, values); Aspose.Words.Node eachSectionNode = eachDoc.Sections[0].Clone(); doc.Sections.Add(doc.ImportNode(eachSectionNode, true)); //回報進度 _BGWDisciplineNotification.ReportProgress((int)(((double)currentStudentCount++ *100.0) / (double)totalStudentNumber)); } #endregion #region 產生學生清單 Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); if (obj.PrintStudentList) { int CountRow = 0; wb.Worksheets[0].Cells[CountRow, 0].PutValue("班級"); wb.Worksheets[0].Cells[CountRow, 1].PutValue("座號"); wb.Worksheets[0].Cells[CountRow, 2].PutValue("學號"); wb.Worksheets[0].Cells[CountRow, 3].PutValue("學生姓名"); wb.Worksheets[0].Cells[CountRow, 4].PutValue("收件人姓名"); wb.Worksheets[0].Cells[CountRow, 5].PutValue("地址"); CountRow++; foreach (string each in StudentSuperOBJ.Keys) { //如果沒有學生就離開 if (obj.PrintHasRecordOnly) { if (StudentSuperOBJ.Count == 0) { continue; } } if (StudentSuperOBJ[each].DemeritStringList.Count == 0) { continue; } wb.Worksheets[0].Cells[CountRow, 0].PutValue(StudentSuperOBJ[each].ClassName); wb.Worksheets[0].Cells[CountRow, 1].PutValue(StudentSuperOBJ[each].SeatNo); wb.Worksheets[0].Cells[CountRow, 2].PutValue(StudentSuperOBJ[each].StudentNumber); wb.Worksheets[0].Cells[CountRow, 3].PutValue(StudentSuperOBJ[each].student.Name); //收件人資料 if (obj.ReceiveName == "監護人姓名") { wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].CustodianName); } else if (obj.ReceiveName == "父親姓名") { wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].FatherName); } else if (obj.ReceiveName == "母親姓名") { wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].MotherName); } else { wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].student.Name); } wb.Worksheets[0].Cells[CountRow, 5].PutValue(StudentSuperOBJ[each].ZipCode + " " + StudentSuperOBJ[each].address); CountRow++; } wb.Worksheets[0].AutoFitColumns(); } #endregion string path = Path.Combine(Application.StartupPath, "Reports"); string path2 = Path.Combine(Application.StartupPath, "Reports"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, reportName + ".doc"); path2 = Path.Combine(path2, reportName + "(學生清單).xls"); string message = "【電子報表通知】您好 本期「{0}」已產生,可於電子報表中檢視「資料期間:{1} 至 {2}」"; e.Result = new object[] { reportName, path, doc, path2, obj.PrintStudentList, wb, string.Format(message, "留察通知單", obj.StartDate.ToShortDateString(), obj.EndDate.ToShortDateString()) }; }
private void _BGWDisciplineNotification_DoWork(object sender, DoWorkEventArgs e) { #region 表頭 GetReduceList(); //獎懲對照表 string reportName = "獎懲通知單"; object[] args = e.Argument as object[]; DateTime startDate = (DateTime)args[0]; DateTime endDate = (DateTime)args[1]; bool printHasRecordOnly = (bool)args[2]; MemoryStream templateStream = (MemoryStream)args[3]; string receiveName = (string)args[4]; string receiveAddress = (string)args[5]; string condName = (string)args[6]; int condNumber = int.Parse((string)args[7]); bool IsInsertDate = (bool)args[8]; bool printStudentList = (bool)args[9]; ChengeDemerit(condName, condNumber); Dictionary <string, int> MDMapping = new Dictionary <string, int>(); MDMapping.Add("大功", 0); MDMapping.Add("小功", 1); MDMapping.Add("嘉獎", 2); MDMapping.Add("大過", 3); MDMapping.Add("小過", 4); MDMapping.Add("警告", 5); int flag = 2; if (!string.IsNullOrEmpty(condName)) { flag = (MDMapping[condName] < 3) ? 1 : 0; } MDFilter filter = new MDFilter(); if (flag < 2) { filter.SetCondition(MDMapping[condName], condNumber); } #endregion #region 快取資訊 //學生資訊 Dictionary <string, Dictionary <string, string> > studentInfo = new Dictionary <string, Dictionary <string, string> >(); //獎懲累計資料 Dictionary <string, Dictionary <string, int> > studentDiscipline = new Dictionary <string, Dictionary <string, int> >(); //獎懲明細 Dictionary <string, List <string> > studentDisciplineDetail = new Dictionary <string, List <string> >(); //所有學生ID List <string> allStudentID = new List <string>(); //學生人數 int currentStudentCount = 1; int totalStudentNumber = 0; //獎勵項目 Dictionary <string, string> meritTable = new Dictionary <string, string>(); meritTable.Add("大功", "A"); meritTable.Add("小功", "B"); meritTable.Add("嘉獎", "C"); //懲戒項目 Dictionary <string, string> demeritTable = new Dictionary <string, string>(); demeritTable.Add("大過", "A"); demeritTable.Add("小過", "B"); demeritTable.Add("警告", "C"); #endregion #region 依據 ClassID 建立班級學生清單 List <StudentRecord> classStudent = SelectedStudents; Dictionary <string, int> StudMeritSum = new Dictionary <string, int>(); foreach (StudentRecord aStudent in classStudent) { string aStudentID = aStudent.ID; if (!StudMeritSum.ContainsKey(aStudentID)) { StudMeritSum.Add(aStudentID, 0); } if (!studentInfo.ContainsKey(aStudentID)) { studentInfo.Add(aStudentID, new Dictionary <string, string>()); } TeacherRecord objT = aStudent.Class == null ? null : aStudent.Class.Teacher; studentInfo[aStudentID].Add("Name", aStudent.Name); studentInfo[aStudentID].Add("EnName", aStudent.EnglishName); studentInfo[aStudentID].Add("ClassName", aStudent.Class == null ? "" : aStudent.Class.Name); if (aStudent.SeatNo.HasValue) { studentInfo[aStudentID].Add("SeatNo", aStudent.SeatNo.Value.ToString()); } else { studentInfo[aStudentID].Add("SeatNo", ""); } studentInfo[aStudentID].Add("StudentNumber", aStudent.StudentNumber); studentInfo[aStudentID].Add("Teacher", objT == null ? "" : objT.Name); if (!studentDiscipline.ContainsKey(aStudentID)) { studentDiscipline.Add(aStudentID, new Dictionary <string, int>()); } if (!studentDisciplineDetail.ContainsKey(aStudentID)) { studentDisciplineDetail.Add(aStudentID, new List <string>()); } if (!allStudentID.Contains(aStudentID)) { allStudentID.Add(aStudentID); } } #endregion //雙語部 - 英文別名欄位 //學生ID : 英文別名 Dictionary <string, string> StudentEXTDic = tool.GetStudentEXT(allStudentID); #region 取得獎懲資料 日期區間 DSXmlHelper helper = new DSXmlHelper("Request"); helper.AddElement("Field"); helper.AddElement("Field", "All"); helper.AddElement("Condition"); foreach (string var in allStudentID) { helper.AddElement("Condition", "RefStudentID", var); } if (IsInsertDate) { helper.AddElement("Condition", "StartDate", startDate.ToShortDateString()); helper.AddElement("Condition", "EndDate", endDate.ToShortDateString()); } else { helper.AddElement("Condition", "StartRegisterDate", startDate.ToShortDateString()); helper.AddElement("Condition", "EndRegisterDate", endDate.ToShortDateString()); } helper.AddElement("Order"); helper.AddElement("Order", "OccurDate", "asc"); DSResponse dsrsp = tool.GetDiscipline(new DSRequest(helper)); foreach (XmlElement var in dsrsp.GetContent().GetElements("Discipline")) { string studentID = var.SelectSingleNode("RefStudentID").InnerText; DateTime occurDate = DateTime.Parse(var.SelectSingleNode("OccurDate").InnerText); string occurMonthDay = occurDate.Month + "/" + occurDate.Day; string reason = var.SelectSingleNode("Reason").InnerText; if (!studentDisciplineDetail.ContainsKey(studentID)) { studentDisciplineDetail.Add(studentID, new List <string>()); } if (!studentDiscipline.ContainsKey(studentID)) { studentDiscipline.Add(studentID, new Dictionary <string, int>()); } if (var.SelectSingleNode("MeritFlag").InnerText == "1") { XmlElement meritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Merit"); if (meritElement == null) { continue; } if (MeritDemerit == "獎勵") { if (StudMeritSum.ContainsKey(studentID)) { StudMeritSum[studentID] += GetMeritType(meritElement); } } bool comma = false; StringBuilder detailString = new StringBuilder(""); detailString.Append(occurMonthDay + " "); if (!string.IsNullOrEmpty(reason)) { detailString.Append(reason + " "); } foreach (string merit in meritTable.Keys) { int tryTimes; int times = int.TryParse(meritElement.GetAttribute(meritTable[merit]), out tryTimes) ? tryTimes : 0; if (times > 0) { if (!studentDiscipline[studentID].ContainsKey("Range" + merit)) { studentDiscipline[studentID].Add("Range" + merit, 0); } studentDiscipline[studentID]["Range" + merit] += times; if (comma) { detailString.Append(","); } detailString.Append(merit + times + "次"); comma = true; } } studentDisciplineDetail[studentID].Add(detailString.ToString()); } else if (var.SelectSingleNode("MeritFlag").InnerText == "0") { XmlElement demeritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Demerit"); if (demeritElement == null) { continue; } bool cleared = false; if (demeritElement.GetAttribute("Cleared") == "是") { cleared = true; } #region 懲戒比例換算 & 判斷 if (cleared == false && MeritDemerit == "懲戒") { if (StudMeritSum.ContainsKey(studentID)) { StudMeritSum[studentID] += GetDemeritType(demeritElement); } } #endregion bool comma = false; StringBuilder detailString = new StringBuilder(""); detailString.Append(occurMonthDay + " "); if (!string.IsNullOrEmpty(reason)) { detailString.Append(reason + " "); } foreach (string demerit in demeritTable.Keys) { int tryTimes; int times = int.TryParse(demeritElement.GetAttribute(demeritTable[demerit]), out tryTimes) ? tryTimes : 0; if (times > 0) { if (!studentDiscipline[studentID].ContainsKey("Range" + demerit)) { studentDiscipline[studentID].Add("Range" + demerit, 0); } if (!cleared) { studentDiscipline[studentID]["Range" + demerit] += times; if (comma) { detailString.Append(","); } detailString.Append(demerit + times + "次"); comma = true; } } } if (!cleared) { studentDisciplineDetail[studentID].Add(detailString.ToString()); } } } #endregion #region 取得獎懲資料 學期累計 helper = new DSXmlHelper("Request"); helper.AddElement("Field"); helper.AddElement("Field", "All"); helper.AddElement("Condition"); foreach (string var in allStudentID) { helper.AddElement("Condition", "RefStudentID", var); } helper.AddElement("Condition", "SchoolYear", School.DefaultSchoolYear); helper.AddElement("Condition", "Semester", School.DefaultSemester); helper.AddElement("Order"); helper.AddElement("Order", "OccurDate", "asc"); dsrsp = tool.GetDiscipline(new DSRequest(helper)); foreach (XmlElement var in dsrsp.GetContent().GetElements("Discipline")) { DateTime occurDate = DateTime.Parse(var.SelectSingleNode("OccurDate").InnerText); if (occurDate.CompareTo(endDate) == 1) { continue; } string studentID = var.SelectSingleNode("RefStudentID").InnerText; if (!studentDiscipline.ContainsKey(studentID)) { studentDiscipline.Add(studentID, new Dictionary <string, int>()); } if (var.SelectSingleNode("MeritFlag").InnerText == "1") { XmlElement meritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Merit"); if (meritElement == null) { continue; } foreach (string merit in meritTable.Keys) { int tryTimes; int times = int.TryParse(meritElement.GetAttribute(meritTable[merit]), out tryTimes) ? tryTimes : 0; if (times > 0) { if (!studentDiscipline[studentID].ContainsKey("Semester" + merit)) { studentDiscipline[studentID].Add("Semester" + merit, 0); } studentDiscipline[studentID]["Semester" + merit] += times; } } } else { XmlElement demeritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Demerit"); if (demeritElement == null) { continue; } bool cleared = false; if (demeritElement.GetAttribute("Cleared") == "是") { cleared = true; } foreach (string demerit in demeritTable.Keys) { int tryTimes; int times = int.TryParse(demeritElement.GetAttribute(demeritTable[demerit]), out tryTimes) ? tryTimes : 0; if (times > 0) { if (!studentDiscipline[studentID].ContainsKey("Semester" + demerit)) { studentDiscipline[studentID].Add("Semester" + demerit, 0); } if (!cleared) { studentDiscipline[studentID]["Semester" + demerit] += times; } } } } } #endregion #region 取得學生通訊地址資料 List <AddressRecord> AddressList = K12.Data.Address.SelectByStudentIDs(allStudentID); foreach (AddressRecord var in AddressList) { string studentID = var.RefStudentID; if (!studentInfo.ContainsKey(studentID)) { studentInfo.Add(studentID, new Dictionary <string, string>()); } studentInfo[studentID].Add("Address", ""); studentInfo[studentID].Add("ZipCode", ""); studentInfo[studentID].Add("ZipCode1", ""); studentInfo[studentID].Add("ZipCode2", ""); studentInfo[studentID].Add("ZipCode3", ""); studentInfo[studentID].Add("ZipCode4", ""); studentInfo[studentID].Add("ZipCode5", ""); if (receiveAddress == "聯絡地址") { if (!string.IsNullOrEmpty(var.MailingAddress)) { studentInfo[studentID]["Address"] = var.MailingCounty + var.MailingTown + var.MailingDistrict + var.MailingArea + var.MailingDetail; } if (!string.IsNullOrEmpty(var.MailingZipCode)) { studentInfo[studentID]["ZipCode"] = var.MailingZipCode; if (var.MailingZipCode.Length >= 1) { studentInfo[studentID]["ZipCode1"] = var.MailingZipCode.Substring(0, 1); } if (var.MailingZipCode.Length >= 2) { studentInfo[studentID]["ZipCode2"] = var.MailingZipCode.Substring(1, 1); } if (var.MailingZipCode.Length >= 3) { studentInfo[studentID]["ZipCode3"] = var.MailingZipCode.Substring(2, 1); } if (var.MailingZipCode.Length >= 4) { studentInfo[studentID]["ZipCode4"] = var.MailingZipCode.Substring(3, 1); } if (var.MailingZipCode.Length >= 5) { studentInfo[studentID]["ZipCode5"] = var.MailingZipCode.Substring(4, 1); } } } else if (receiveAddress == "其他地址") { if (!string.IsNullOrEmpty(var.Address1Address)) { studentInfo[studentID]["Address"] = var.Address1County + var.Address1Town + var.Address1District + var.Address1Area + var.Address1Detail; } if (!string.IsNullOrEmpty(var.Address1ZipCode)) { studentInfo[studentID]["ZipCode"] = var.Address1ZipCode; if (var.Address1ZipCode.Length >= 1) { studentInfo[studentID]["ZipCode1"] = var.Address1ZipCode.Substring(0, 1); } if (var.Address1ZipCode.Length >= 2) { studentInfo[studentID]["ZipCode2"] = var.Address1ZipCode.Substring(1, 1); } if (var.Address1ZipCode.Length >= 3) { studentInfo[studentID]["ZipCode3"] = var.Address1ZipCode.Substring(2, 1); } if (var.Address1ZipCode.Length >= 4) { studentInfo[studentID]["ZipCode4"] = var.Address1ZipCode.Substring(3, 1); } if (var.Address1ZipCode.Length >= 5) { studentInfo[studentID]["ZipCode5"] = var.Address1ZipCode.Substring(4, 1); } } } else //戶籍地址 { if (!string.IsNullOrEmpty(var.PermanentAddress)) { studentInfo[studentID]["Address"] = var.PermanentCounty + var.PermanentTown + var.PermanentDistrict + var.PermanentArea + var.PermanentDetail; } if (!string.IsNullOrEmpty(var.PermanentZipCode)) { studentInfo[studentID]["ZipCode"] = var.PermanentZipCode; if (var.PermanentZipCode.Length >= 1) { studentInfo[studentID]["ZipCode1"] = var.PermanentZipCode.Substring(0, 1); } if (var.PermanentZipCode.Length >= 2) { studentInfo[studentID]["ZipCode2"] = var.PermanentZipCode.Substring(1, 1); } if (var.PermanentZipCode.Length >= 3) { studentInfo[studentID]["ZipCode3"] = var.PermanentZipCode.Substring(2, 1); } if (var.PermanentZipCode.Length >= 4) { studentInfo[studentID]["ZipCode4"] = var.PermanentZipCode.Substring(3, 1); } if (var.PermanentZipCode.Length >= 5) { studentInfo[studentID]["ZipCode5"] = var.PermanentZipCode.Substring(4, 1); } } } } #endregion #region 取得學生監護人父母親資料 dsrsp = tool.GetMultiParentInfo(allStudentID.ToArray()); foreach (XmlElement var in dsrsp.GetContent().GetElements("ParentInfo")) { string studentID = var.GetAttribute("StudentID"); studentInfo[studentID].Add("CustodianName", var.SelectSingleNode("CustodianName").InnerText); studentInfo[studentID].Add("FatherName", var.SelectSingleNode("FatherName").InnerText); studentInfo[studentID].Add("MotherName", var.SelectSingleNode("MotherName").InnerText); } #endregion #region 產生報表 Aspose.Words.Document template = new Aspose.Words.Document(templateStream, "", Aspose.Words.LoadFormat.Doc, ""); template.MailMerge.Execute( new string[] { "學校名稱", "學校地址", "學校電話", "學校英文名稱" }, new object[] { School.ChineseName, School.Address, School.Telephone, School.EnglishName } ); Aspose.Words.Document doc = new Aspose.Words.Document(); doc.RemoveAllChildren(); Aspose.Words.Node sectionNode = template.Sections[0].Clone(); totalStudentNumber = studentDiscipline.Count; foreach (string student in studentDiscipline.Keys) { if (printHasRecordOnly) { if (studentDisciplineDetail[student].Count == 0) { continue; } } if (MeritDemerit == "獎勵") { if (StudMeritSum[student] < MaxMerit) { continue; } } else if (MeritDemerit == "懲戒") { if (StudMeritSum[student] < MaxDemerit) { continue; } } else //未設定 { } #region 過濾不需要列印的學生 if (flag < 2) { int A = 0, B = 0, C = 0; if (flag == 1) { int tryM; A = studentDiscipline[student].TryGetValue("Range大功", out tryM) ? tryM : 0; B = studentDiscipline[student].TryGetValue("Range小功", out tryM) ? tryM : 0; C = studentDiscipline[student].TryGetValue("Range嘉獎", out tryM) ? tryM : 0; } else if (flag == 0) { int tryD; A = studentDiscipline[student].TryGetValue("Range大過", out tryD) ? tryD : 0; B = studentDiscipline[student].TryGetValue("Range小過", out tryD) ? tryD : 0; C = studentDiscipline[student].TryGetValue("Range警告", out tryD) ? tryD : 0; } //if (filter.IsFilter(A, B, C)) // continue; } #endregion Aspose.Words.Document eachDoc = new Aspose.Words.Document(); eachDoc.RemoveAllChildren(); eachDoc.Sections.Add(eachDoc.ImportNode(sectionNode, true)); //合併列印的資料 Dictionary <string, object> mapping = new Dictionary <string, object>(); Dictionary <string, string> eachStudentInfo = studentInfo[student]; XmlElement SchoolXml = K12.Data.School.Configuration["學校資訊"].PreviousData; XmlElement SchoolXml2 = (XmlElement)SchoolXml.SelectSingleNode("EnglishAddress"); if (SchoolXml2 != null) { mapping.Add("學校英文地址", SchoolXml2.InnerText); } else { mapping.Add("學校英文地址", ""); } //學生資料 mapping.Add("學生姓名", eachStudentInfo["Name"]); mapping.Add("英文姓名", eachStudentInfo["EnName"]); mapping.Add("班級", eachStudentInfo["ClassName"]); mapping.Add("座號", eachStudentInfo["SeatNo"]); mapping.Add("學號", eachStudentInfo["StudentNumber"]); mapping.Add("導師", eachStudentInfo["Teacher"]); mapping.Add("資料期間", startDate.ToShortDateString() + " 至 " + endDate.ToShortDateString()); //收件人資料 if (receiveName == "監護人姓名") { mapping.Add("收件人姓名", eachStudentInfo["CustodianName"]); } else if (receiveName == "父親姓名") { mapping.Add("收件人姓名", eachStudentInfo["FatherName"]); } else if (receiveName == "母親姓名") { mapping.Add("收件人姓名", eachStudentInfo["MotherName"]); } else { mapping.Add("收件人姓名", eachStudentInfo["Name"]); } //收件人地址資料 mapping.Add("收件人地址", eachStudentInfo["Address"]); mapping.Add("郵遞區號", eachStudentInfo["ZipCode"]); mapping.Add("0", eachStudentInfo["ZipCode1"]); mapping.Add("1", eachStudentInfo["ZipCode2"]); mapping.Add("2", eachStudentInfo["ZipCode3"]); mapping.Add("4", eachStudentInfo["ZipCode4"]); mapping.Add("5", eachStudentInfo["ZipCode5"]); if (StudentEXTDic.ContainsKey(student)) { mapping.Add("英文別名", StudentEXTDic[student]); } else { mapping.Add("英文別名", ""); } mapping.Add("學年度", tool.GetSchoolChange(School.DefaultSchoolYear)); mapping.Add("學期", School.DefaultSemester); Dictionary <string, int> eachStudentDiscipline = studentDiscipline[student]; //學生獎懲累計資料 int count; mapping.Add("學期累計大功", eachStudentDiscipline.TryGetValue("Semester大功", out count) ? "" + count : "0"); mapping.Add("學期累計小功", eachStudentDiscipline.TryGetValue("Semester小功", out count) ? "" + count : "0"); mapping.Add("學期累計嘉獎", eachStudentDiscipline.TryGetValue("Semester嘉獎", out count) ? "" + count : "0"); mapping.Add("學期累計大過", eachStudentDiscipline.TryGetValue("Semester大過", out count) ? "" + count : "0"); mapping.Add("學期累計小過", eachStudentDiscipline.TryGetValue("Semester小過", out count) ? "" + count : "0"); mapping.Add("學期累計警告", eachStudentDiscipline.TryGetValue("Semester警告", out count) ? "" + count : "0"); mapping.Add("本期累計大功", eachStudentDiscipline.TryGetValue("Range大功", out count) ? "" + count : "0"); mapping.Add("本期累計小功", eachStudentDiscipline.TryGetValue("Range小功", out count) ? "" + count : "0"); mapping.Add("本期累計嘉獎", eachStudentDiscipline.TryGetValue("Range嘉獎", out count) ? "" + count : "0"); mapping.Add("本期累計大過", eachStudentDiscipline.TryGetValue("Range大過", out count) ? "" + count : "0"); mapping.Add("本期累計小過", eachStudentDiscipline.TryGetValue("Range小過", out count) ? "" + count : "0"); mapping.Add("本期累計警告", eachStudentDiscipline.TryGetValue("Range警告", out count) ? "" + count : "0"); //獎懲明細 object[] objectValues = new object[] { studentDisciplineDetail[student] }; mapping.Add("獎懲明細", objectValues); string[] keys = new string[mapping.Count]; object[] values = new object[mapping.Count]; int i = 0; foreach (string key in mapping.Keys) { keys[i] = key; values[i++] = mapping[key]; } //合併列印 eachDoc.MailMerge.MergeField += new Aspose.Words.Reporting.MergeFieldEventHandler(DisciplineNotification_MailMerge_MergeField); eachDoc.MailMerge.RemoveEmptyParagraphs = true; eachDoc.MailMerge.Execute(keys, values); Aspose.Words.Node eachSectionNode = eachDoc.Sections[0].Clone(); doc.Sections.Add(doc.ImportNode(eachSectionNode, true)); //回報進度 _BGWDisciplineNotification.ReportProgress((int)(((double)currentStudentCount++ *100.0) / (double)totalStudentNumber)); } #endregion #region 產生學生清單 Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(); if (printStudentList) { if (printHasRecordOnly) { int CountRow = 0; wb.Worksheets[0].Cells[CountRow, 0].PutValue("班級"); wb.Worksheets[0].Cells[CountRow, 1].PutValue("座號"); wb.Worksheets[0].Cells[CountRow, 2].PutValue("學號"); wb.Worksheets[0].Cells[CountRow, 3].PutValue("學生姓名"); wb.Worksheets[0].Cells[CountRow, 4].PutValue("英文姓名"); wb.Worksheets[0].Cells[CountRow, 5].PutValue("英文別名"); wb.Worksheets[0].Cells[CountRow, 6].PutValue("收件人姓名"); wb.Worksheets[0].Cells[CountRow, 7].PutValue("地址"); CountRow++; foreach (string each in studentInfo.Keys) { if (studentDisciplineDetail[each].Count == 0) { continue; } if (MeritDemerit == "獎勵") { if (StudMeritSum[each] < MaxMerit) { continue; } } else if (MeritDemerit == "懲戒") { if (StudMeritSum[each] < MaxDemerit) { continue; } } else //未設定 { } wb.Worksheets[0].Cells[CountRow, 0].PutValue(studentInfo[each]["ClassName"]); wb.Worksheets[0].Cells[CountRow, 1].PutValue(studentInfo[each]["SeatNo"]); wb.Worksheets[0].Cells[CountRow, 2].PutValue(studentInfo[each]["StudentNumber"]); wb.Worksheets[0].Cells[CountRow, 3].PutValue(studentInfo[each]["Name"]); wb.Worksheets[0].Cells[CountRow, 4].PutValue(studentInfo[each]["EnName"]); wb.Worksheets[0].Cells[CountRow, 5].PutValue(studentInfo[each]["ExName"]); //收件人資料 if (receiveName == "監護人姓名") { wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["CustodianName"]); } else if (receiveName == "父親姓名") { wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["FatherName"]); } else if (receiveName == "母親姓名") { wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["MotherName"]); } else { wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["Name"]); } wb.Worksheets[0].Cells[CountRow, 7].PutValue(studentInfo[each]["ZipCode"] + " " + studentInfo[each]["Address"]); CountRow++; } wb.Worksheets[0].AutoFitColumns(); } } #endregion string path = Path.Combine(Application.StartupPath, "Reports"); string path2 = Path.Combine(Application.StartupPath, "Reports"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, reportName + ".doc"); path2 = Path.Combine(path2, reportName + "(學生清單).xls"); e.Result = new object[] { reportName, path, doc, path2, printStudentList, wb }; }