void BGW_DoWork(object sender, DoWorkEventArgs e) { ReportConfiguration ConfigurationInCadre = new ReportConfiguration(ClassPrint_Config_1); Aspose.Words.Document Template; if (ConfigurationInCadre.Template == null) { //如果範本為空,則建立一個預設範本 ReportConfiguration ConfigurationInCadre_1 = new ReportConfiguration(ClassPrint_Config_1); ConfigurationInCadre_1.Template = new ReportTemplate(Properties.Resources.社團點名單_套表列印1, TemplateType.docx); //ConfigurationInCadre_1.Template = new Campus.Report.ReportTemplate(Properties.Resources.社團點名表_合併欄位總表, Campus.Report.TemplateType.Word); //Jean Template = new Document(ConfigurationInCadre_1.Template.GetStream()); } else { //如果已有範本,則取得樣板 Template = new Document(ConfigurationInCadre.Template.GetStream()); } string[] fieldNames = Template.MailMerge.GetFieldNames(); foreach (var item in fieldNames) { if (item.Contains("姓名")) { 學生多少個++; } } SCJoinDataLoad crM = new SCJoinDataLoad(); #region 日期 List <string> config = new List <string>(); XmlElement day = (XmlElement)e.Argument; if (day == null) { MsgBox.Show("第一次使用報表請先進行[日期設定]"); return; } else { config.Clear(); foreach (XmlElement xml in day.SelectNodes("item")) { config.Add(xml.InnerText); } } #endregion DataTable table = new DataTable(); table.Columns.Add("學校名稱"); table.Columns.Add("社團名稱"); table.Columns.Add("學年度"); table.Columns.Add("學期"); table.Columns.Add("上課地點"); table.Columns.Add("社團類型"); table.Columns.Add("社團代碼"); //2016/9/22 - 新增 table.Columns.Add("指導老師1"); table.Columns.Add("指導老師2"); table.Columns.Add("指導老師3"); table.Columns.Add("列印日期"); table.Columns.Add("上課開始"); table.Columns.Add("上課結束"); table.Columns.Add("人數"); for (int x = 1; x <= 日期多少天; x++) { table.Columns.Add(string.Format("日期_{0}", x)); } for (int x = 1; x <= 學生多少個; x++) { table.Columns.Add(string.Format("班級_{0}", x)); } for (int x = 1; x <= 學生多少個; x++) { table.Columns.Add(string.Format("座號_{0}", x)); } for (int x = 1; x <= 學生多少個; x++) { table.Columns.Add(string.Format("姓名_{0}", x)); } for (int x = 1; x <= 學生多少個; x++) { table.Columns.Add(string.Format("學號_{0}", x)); } for (int x = 1; x <= 學生多少個; x++) { table.Columns.Add(string.Format("性別_{0}", x)); } foreach (string each in crM.CLUBRecordDic.Keys) { //社團資料 CLUBRecord cr = crM.CLUBRecordDic[each]; #region row 各種基本資料 DataRow row = table.NewRow(); row["學校名稱"] = K12.Data.School.ChineseName; row["社團名稱"] = cr.ClubName; row["學年度"] = cr.SchoolYear; row["學期"] = cr.Semester; row["上課地點"] = cr.Location; row["社團類型"] = cr.ClubCategory; row["社團代碼"] = cr.ClubNumber; if (crM.TeacherDic.ContainsKey(cr.RefTeacherID)) { row["指導老師1"] = crM.TeacherDic[cr.RefTeacherID].Name; } if (crM.TeacherDic.ContainsKey(cr.RefTeacherID2)) { row["指導老師2"] = crM.TeacherDic[cr.RefTeacherID2].Name; } if (crM.TeacherDic.ContainsKey(cr.RefTeacherID3)) { row["指導老師3"] = crM.TeacherDic[cr.RefTeacherID3].Name; } //row["外聘老師"] = ""; row["列印日期"] = DateTime.Today.ToShortDateString(); row["上課開始"] = config[0]; row["上課結束"] = config[config.Count - 1]; row["人數"] = crM.ClubByStudentList[each].Count; for (int x = 1; x <= config.Count; x++) { row[string.Format("日期_{0}", x)] = config[x - 1]; } #endregion int y = 1; foreach (StudentRecord obj in crM.ClubByStudentList[each]) { if (y <= 學生多少個) //限制畫面到100名學生 { row[string.Format("班級_{0}", y)] = obj.Class != null ? obj.Class.Name : ""; row[string.Format("座號_{0}", y)] = obj.SeatNo.HasValue ? obj.SeatNo.Value.ToString() : ""; row[string.Format("姓名_{0}", y)] = obj.Name; row[string.Format("學號_{0}", y)] = obj.StudentNumber; row[string.Format("性別_{0}", y)] = obj.Gender; y++; if (y == (學生多少個 + 1)) { y = 1; table.Rows.Add(row); row = table.NewRow(); #region row 各種基本資料 row["學校名稱"] = K12.Data.School.ChineseName; row["社團名稱"] = cr.ClubName; row["學年度"] = cr.SchoolYear; row["學期"] = cr.Semester; row["上課地點"] = cr.Location; row["社團類型"] = cr.ClubCategory; row["社團代碼"] = cr.ClubNumber; if (crM.TeacherDic.ContainsKey(cr.RefTeacherID)) { row["指導老師1"] = crM.TeacherDic[cr.RefTeacherID].Name; } if (crM.TeacherDic.ContainsKey(cr.RefTeacherID2)) { row["指導老師2"] = crM.TeacherDic[cr.RefTeacherID2].Name; } if (crM.TeacherDic.ContainsKey(cr.RefTeacherID3)) { row["指導老師3"] = crM.TeacherDic[cr.RefTeacherID3].Name; } //row["外聘老師"] = ""; row["列印日期"] = DateTime.Today.ToShortDateString(); row["上課開始"] = config[0]; row["上課結束"] = config[config.Count - 1]; row["人數"] = crM.ClubByStudentList[each].Count; for (int x = 1; x <= config.Count; x++) { row[string.Format("日期_{0}", x)] = config[x - 1]; } #endregion } } } table.Rows.Add(row); } Document PageOne = (Document)Template.Clone(true); PageOne.MailMerge.Execute(table); e.Result = PageOne; }
void BGW_DoWork(object sender, DoWorkEventArgs e) { //建立社團比對學生ID的清單 //以每個社團為一個單位 //進行學生資料列印 SDL = new SCJoinDataLoad(); //建立Excel範本 Workbook template = new Workbook(); //Jean Open template.Open(new MemoryStream(Properties.Resources.社團點名單_範本), FileFormatType.Excel97To2003); //每一張 Workbook prototype = new Workbook(); prototype.Copy(template); Worksheet ptws = prototype.Worksheets[0]; //範圍 Range ptHeader = ptws.Cells.CreateRange(0, 4, false); Range ptEachRow = ptws.Cells.CreateRange(4, 1, false); //儲存資料 Workbook wb = new Workbook(); wb.Copy(prototype); //取得Sheet Worksheet ws = wb.Worksheets[0]; int index = 0; int dataIndex = 0; //每一個社團 foreach (string club in SDL.ClubByStudentList.Keys) { //社團資訊收集 CLUBRecord cr = SDL.CLUBRecordDic[club]; //社團標頭 string TitleName1 = string.Format("{0}學年度/第{1}學期 社團點名單", cr.SchoolYear.ToString(), cr.Semester.ToString()); string TitleName2 = cr.ClubName + " (類型:" + cr.ClubCategory + ")"; ws.Cells.CreateRange(dataIndex, 4, false).CopyStyle(ptHeader); ws.Cells.CreateRange(dataIndex, 4, false).CopyValue(ptHeader); ws.Cells[dataIndex, 0].PutValue(TitleName1); dataIndex += 1; ws.Cells[dataIndex, 0].PutValue(TitleName2); dataIndex += 1; ws.Cells[dataIndex, 0].PutValue("代碼:" + cr.ClubNumber); ws.Cells[dataIndex, 1].PutValue("場地:" + cr.Location); ws.Cells[dataIndex, 3].PutValue("人數:" + SDL.ClubByStudentList[club].Count); //社團老師 string TeacherNameA = GetTeacherName(cr.RefTeacherID); string TeacherNameB = GetTeacherName(cr.RefTeacherID2); string TeacherNameC = GetTeacherName(cr.RefTeacherID3); string TeacherName = "老師:" + TeacherNameA + " " + TeacherNameB + " " + TeacherNameC; ws.Cells[dataIndex, 4].PutValue(TeacherName); dataIndex += 2; foreach (StudentRecord stud in SDL.ClubByStudentList[club]) { ws.Cells.CreateRange(dataIndex, 1, false).CopyStyle(ptEachRow); ws.Cells.CreateRange(dataIndex, 1, false).CopyValue(ptEachRow); string classname = string.IsNullOrEmpty(stud.RefClassID) ? "" : stud.Class.Name; ws.Cells[dataIndex, 0].PutValue(classname); ws.Cells[dataIndex, 1].PutValue(stud.SeatNo.HasValue ? stud.SeatNo.Value.ToString() : ""); ws.Cells[dataIndex, 2].PutValue(stud.Name); ws.Cells[dataIndex, 3].PutValue(stud.StudentNumber); ws.Cells[dataIndex, 4].PutValue(stud.Gender); dataIndex += 1; } ws.Cells.CreateRange(dataIndex - 1, 0, 1, 6).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black); ws.HPageBreaks.Add(dataIndex, 6); } e.Result = wb; }