private void BgWorkerExport_DoWork(object sender, DoWorkEventArgs e) { bgWorkerExport.ReportProgress(1); try { // 取得預設樣板 Workbook wb = new Workbook(new MemoryStream(Properties.Resources.Template)); Worksheet wst = wb.Worksheets[0]; // 取得學生基本資料 List <StudentInfo> StudentInfoList = QueryData.GetStudentInfoList3(); List <string> StudentIDList = new List <string>(); foreach (StudentInfo si in StudentInfoList) { StudentIDList.Add(si.StudentID); } // 取得地址資訊 Dictionary <string, JHAddressRecord> AddressDict = new Dictionary <string, JHAddressRecord>(); List <JHAddressRecord> tmpAddress = JHAddress.SelectByStudentIDs(StudentIDList); foreach (JHAddressRecord rec in tmpAddress) { if (!AddressDict.ContainsKey(rec.RefStudentID)) { AddressDict.Add(rec.RefStudentID, rec); } } // 取得電話資料 Dictionary <string, JHPhoneRecord> PhoneDict = new Dictionary <string, JHPhoneRecord>(); List <JHPhoneRecord> tmpPhone = JHPhone.SelectByStudentIDs(StudentIDList); foreach (JHPhoneRecord rec in tmpPhone) { if (!PhoneDict.ContainsKey(rec.RefStudentID)) { PhoneDict.Add(rec.RefStudentID, rec); } } bgWorkerExport.ReportProgress(20); // 取得監護人父母資訊 Dictionary <string, JHParentRecord> ParentDict = new Dictionary <string, JHParentRecord>(); List <JHParentRecord> tmpParent = JHParent.SelectByStudentIDs(StudentIDList); foreach (JHParentRecord rec in tmpParent) { if (!ParentDict.ContainsKey(rec.RefStudentID)) { ParentDict.Add(rec.RefStudentID, rec); } } // 轉換各項類別對照值 Dictionary <string, string> MappingTag1 = new Dictionary <string, string>(); Dictionary <string, string> MappingTag2 = new Dictionary <string, string>(); Dictionary <string, string> MappingTag3 = new Dictionary <string, string>(); Dictionary <string, string> MappingTag4 = new Dictionary <string, string>(); // 取得學生類別 Dictionary <string, List <string> > StudentTagDict = QueryData.GetStudentTagName(StudentIDList); // 解析對照設定 XElement elmRoot = XElement.Parse(_Configure.MappingContent); if (elmRoot != null) { foreach (XElement elm in elmRoot.Elements("Group")) { string gpName = elm.Attribute("Name").Value; if (gpName == "學生身分") { foreach (XElement elm1 in elm.Elements("Item")) { string tagName = elm1.Attribute("TagName").Value; if (!MappingTag1.ContainsKey(tagName) && tagName.Length > 0) { MappingTag1.Add(tagName, elm1.Attribute("Code").Value); } } } if (gpName == "身心障礙") { foreach (XElement elm1 in elm.Elements("Item")) { string tagName = elm1.Attribute("TagName").Value; if (!MappingTag2.ContainsKey(tagName) && tagName.Length > 0) { MappingTag2.Add(tagName, elm1.Attribute("Code").Value); } } } if (gpName == "學生報名身分設定") { foreach (XElement elm1 in elm.Elements("Item")) { string tagName = elm1.Attribute("TagName").Value; if (!MappingTag3.ContainsKey(tagName) && tagName.Length > 0) { MappingTag3.Add(tagName, elm1.Attribute("Code").Value); } } } if (gpName == "失業勞工子女") { foreach (XElement elm1 in elm.Elements("Item")) { string tagName = elm1.Attribute("TagName").Value; if (!MappingTag4.ContainsKey(tagName) && tagName.Length > 0) { MappingTag4.Add(tagName, elm1.Attribute("Code").Value); } } } } } bgWorkerExport.ReportProgress(40); // 取得成績相關資料 Dictionary <string, List <JHSemesterScoreRecord> > SemesterScoreRecordDict = new Dictionary <string, List <JHSemesterScoreRecord> >(); List <JHSemesterScoreRecord> tmpSemsScore = JHSemesterScore.SelectByStudentIDs(StudentIDList); foreach (JHSemesterScoreRecord rec in tmpSemsScore) { if (!SemesterScoreRecordDict.ContainsKey(rec.RefStudentID)) { SemesterScoreRecordDict.Add(rec.RefStudentID, new List <JHSemesterScoreRecord>()); } SemesterScoreRecordDict[rec.RefStudentID].Add(rec); } // 取得功過紀錄 // 功過對照表 JHMeritDemeritReduceRecord DemeritReduceRecord = JHMeritDemeritReduce.Select(); // 懲 Dictionary <string, List <JHDemeritRecord> > DemeritRecordDict = new Dictionary <string, List <JHDemeritRecord> >(); List <JHDemeritRecord> tmpDemeritRecord = JHDemerit.SelectByStudentIDs(StudentIDList); foreach (JHDemeritRecord rec in tmpDemeritRecord) { if (rec.Cleared == "是") { continue; } if (rec.OccurDate > _Configure.EndDate) { continue; } else { if (!DemeritRecordDict.ContainsKey(rec.RefStudentID)) { DemeritRecordDict.Add(rec.RefStudentID, new List <JHDemeritRecord>()); } DemeritRecordDict[rec.RefStudentID].Add(rec); } } // 獎 Dictionary <string, List <JHMeritRecord> > MeritRecordDict = new Dictionary <string, List <JHMeritRecord> >(); List <JHMeritRecord> tmpMeritRecord = JHMerit.SelectByStudentIDs(StudentIDList); foreach (JHMeritRecord rec in tmpMeritRecord) { if (rec.OccurDate > _Configure.EndDate) { continue; } else { if (!MeritRecordDict.ContainsKey(rec.RefStudentID)) { MeritRecordDict.Add(rec.RefStudentID, new List <JHMeritRecord>()); } MeritRecordDict[rec.RefStudentID].Add(rec); } } // 取得服務學習時數 StudentInfoList = QueryData.FillServiceLearn(StudentIDList, StudentInfoList, _Configure.EndDate); // 填入中低收入戶 StudentInfoList = QueryData.FillIncomeType(StudentIDList, StudentInfoList); // 取得學生體適能資料並填入,嘉義版不卡日期,日期傳入不會限制 StudentInfoList = QueryData.FillStudentFitness(StudentIDList, StudentInfoList, _Configure.EndDate); // 取得競賽總積分並填入學生資料 StudentInfoList = QueryData.FillStudentCompetitionPerformanceSum(StudentIDList, StudentInfoList); // 填入 Excel 資料 int wstRIdx = 1; bgWorkerExport.ReportProgress(70); foreach (StudentInfo si in StudentInfoList) { // 考區代碼 0,嘉義區 10 wst.Cells[wstRIdx, 0].PutValue(10); // 集報單位代碼 1 wst.Cells[wstRIdx, 1].PutValue(K12.Data.School.Code); // 序號 2 wst.Cells[wstRIdx, 2].PutValue(wstRIdx); // 學號 3 wst.Cells[wstRIdx, 3].PutValue(si.StudentNumber); // 班級 4 wst.Cells[wstRIdx, 4].PutValue(si.ClassName); // 座號 5 wst.Cells[wstRIdx, 5].PutValue(si.SeatNo); // 學生姓名 6 // 2021-12-27 有些學校會為了整齊把學生的名字+空白,這裡濾掉 wst.Cells[wstRIdx, 6].PutValue(si.StudentName.Replace(" ", "").Replace(" ", "")); // 身分證統一編號 7 wst.Cells[wstRIdx, 7].PutValue(si.IDNumber); // 非中華民國身分證號 8 if (si.isTaiwanID) { wst.Cells[wstRIdx, 8].PutValue(""); } else { wst.Cells[wstRIdx, 8].PutValue("V"); } // 性別 9 wst.Cells[wstRIdx, 9].PutValue(si.GenderCode); // 出生年(民國年) 10 wst.Cells[wstRIdx, 10].PutValue(si.BirthYear); // 出生月 11 wst.Cells[wstRIdx, 11].PutValue(si.BirthMonth); // 出生日 12 wst.Cells[wstRIdx, 12].PutValue(si.BirthDay); // 畢業學校代碼 13 wst.Cells[wstRIdx, 13].PutValue(K12.Data.School.Code); // 畢業年(民國年) 14 int gyear; if (int.TryParse(K12.Data.School.DefaultSchoolYear, out gyear)) { wst.Cells[wstRIdx, 14].PutValue(gyear + 1); } // 畢肄業 15 wst.Cells[wstRIdx, 15].PutValue(1); wst.Cells[wstRIdx, 16].PutValue(0); wst.Cells[wstRIdx, 17].PutValue(0); wst.Cells[wstRIdx, 18].PutValue(0); wst.Cells[wstRIdx, 22].PutValue(0); if (StudentTagDict.ContainsKey(si.StudentID)) { foreach (string tagName in StudentTagDict[si.StudentID]) { if (MappingTag1.ContainsKey(tagName)) { // 學生身分 16 wst.Cells[wstRIdx, 16].PutValue(MappingTag1[tagName]); } if (MappingTag2.ContainsKey(tagName)) { si.isSpecial = true; // 身心障礙 18 wst.Cells[wstRIdx, 18].PutValue(MappingTag2[tagName]); } if (MappingTag3.ContainsKey(tagName)) { // 學生報名身分 17 wst.Cells[wstRIdx, 17].PutValue(MappingTag3[tagName]); } if (MappingTag4.ContainsKey(tagName)) { // 失業勞工子女 22 wst.Cells[wstRIdx, 22].PutValue(MappingTag4[tagName]); } } } // 就學區 19,不處理 // 低收入戶 20 if (si.incomeType1) { wst.Cells[wstRIdx, 20].PutValue(1); } else { wst.Cells[wstRIdx, 20].PutValue(0); } // 中低收入戶 21 if (si.incomeType2) { wst.Cells[wstRIdx, 21].PutValue(1); } else { wst.Cells[wstRIdx, 21].PutValue(0); } // 資料授權 23 wst.Cells[wstRIdx, 23].PutValue(0); string parentName = ""; // 家長姓名 24 if (ParentDict.ContainsKey(si.StudentID)) { if (!string.IsNullOrWhiteSpace(ParentDict[si.StudentID].CustodianName)) { parentName = ParentDict[si.StudentID].CustodianName; } else if (!string.IsNullOrWhiteSpace(ParentDict[si.StudentID].FatherName)) { parentName = ParentDict[si.StudentID].FatherName; } else if (!string.IsNullOrWhiteSpace(ParentDict[si.StudentID].MotherName)) { parentName = ParentDict[si.StudentID].MotherName; } else { } wst.Cells[wstRIdx, 24].PutValue(parentName); } // 市內電話 25 // 市內電話分機 26 // 行動電話 27 if (PhoneDict.ContainsKey(si.StudentID)) { wst.Cells[wstRIdx, 25].PutValue(PhoneDict[si.StudentID].Contact.Replace("-", "").Replace(")", "").Replace("(", "")); wst.Cells[wstRIdx, 27].PutValue(PhoneDict[si.StudentID].Cell.Replace("-", "").Replace(")", "").Replace("(", "")); } // 郵遞區號 28 if (AddressDict.ContainsKey(si.StudentID)) { if (AddressDict[si.StudentID].MailingZipCode != null) { string zipCode = AddressDict[si.StudentID].MailingZipCode; if (zipCode.Length >= 3) { zipCode = zipCode.Substring(0, 3); } wst.Cells[wstRIdx, 28].PutValue(zipCode); } // 通訊地址 29 wst.Cells[wstRIdx, 29].PutValue(AddressDict[si.StudentID].MailingCounty + AddressDict[si.StudentID].MailingTown + AddressDict[si.StudentID].MailingDistrict + AddressDict[si.StudentID].MailingArea + AddressDict[si.StudentID].MailingDetail); } // 計算分數 if (SemesterScoreRecordDict.ContainsKey(si.StudentID)) { si.CalcSemsScore5(SemesterScoreRecordDict[si.StudentID]); } // 健康與體育 30 if (si.isDomainHelPass) { wst.Cells[wstRIdx, 30].PutValue(1); } else { wst.Cells[wstRIdx, 30].PutValue(0); } // 藝術 31 if (si.isDoaminArtPass) { wst.Cells[wstRIdx, 31].PutValue(1); } else { wst.Cells[wstRIdx, 31].PutValue(0); } // 綜合活動 32 if (si.isDomainActPass) { wst.Cells[wstRIdx, 32].PutValue(1); } else { wst.Cells[wstRIdx, 32].PutValue(0); } // 品德表現 33 List <JHDemeritRecord> recD; List <JHMeritRecord> recM; if (DemeritRecordDict.ContainsKey(si.StudentID)) { recD = DemeritRecordDict[si.StudentID]; } else { recD = new List <JHDemeritRecord>(); } if (MeritRecordDict.ContainsKey(si.StudentID)) { recM = MeritRecordDict[si.StudentID]; } else { recM = new List <JHMeritRecord>(); } si.CalcDemeritMemeritScore(recD, recM, DemeritReduceRecord); wst.Cells[wstRIdx, 33].PutValue(si.MeritDemeritScore); // 服務學習 34 wst.Cells[wstRIdx, 34].PutValue(si.ServiceLearnScore); // 體適能 35 si.CalcFitnessScore(); wst.Cells[wstRIdx, 35].PutValue(si.FitnessScore); // 競賽表現 36,使用者自行處理 if (si.CompPerfSum.HasValue) { wst.Cells[wstRIdx, 36].PutValue(si.CompPerfSum.Value); } else // 2021-12-27 嘉義協同國中 見安老師要求預設值 0 { wst.Cells[wstRIdx, 36].PutValue("0"); } // 2021-12-27 嘉義協同國中 見安老師要求預設值全部填1 // https://3.basecamp.com/4399967/buckets/15765350/todos/4475736499#__recording_4477516175 // 家長意見_高中 37 wst.Cells[wstRIdx, 37].PutValue("1"); // 家長意見_高職 38 wst.Cells[wstRIdx, 38].PutValue("1"); // 導師意見_高中 39 wst.Cells[wstRIdx, 39].PutValue("1"); // 導師意見_高職 40 wst.Cells[wstRIdx, 40].PutValue("1"); // 輔導教師意見_高中 41 wst.Cells[wstRIdx, 41].PutValue("1"); // 輔導教師意見_高職 42 wst.Cells[wstRIdx, 42].PutValue("1"); wstRIdx++; } bgWorkerExport.ReportProgress(100); e.Result = wb; } catch (Exception ex) { throw ex; } }