コード例 #1
0
        void _bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            _studDataDict.Clear();
            _dataDictList.Clear();
            string SchoolName = School.ChineseName;

            SaveConfig(是否使用範本);

            Dictionary <int, string> intTeacherNameDict = new Dictionary <int, string>();

            foreach (TeacherRecord tr in Teacher.SelectAll())
            {
                if (tr.Status == TeacherRecord.TeacherStatus.刪除)
                {
                    continue;
                }

                int    tid   = int.Parse(tr.ID);
                string TName = tr.Name;
                if (!string.IsNullOrWhiteSpace(tr.Nickname))
                {
                    TName = tr.Name + "(" + tr.Nickname + ")";
                }

                intTeacherNameDict.Add(tid, TName);
            }

            if (_UserSelectType == SelectType.學生)
            {
                #region 學生
                // 取得學生資料
                foreach (StudentRecord stud in Student.SelectByIDs(_studIDList))
                {
                    _studDataDict.Add(stud.ID, stud);
                }

                // 取得晤談紀錄
                List <UDT_CounselStudentInterviewRecordDef> dataList = _UDTTransfer.GetCounselStudentInterviewRecordByStudentIDList(_studIDList);

                // 透過 query 依照班級序號、班級名稱、學號座號排序
                _studIDList = Utility.SortStudentID1(_studIDList);

                foreach (string studID in _studIDList)
                {
                    int    sid        = int.Parse(studID);
                    string studName   = "";
                    string StudNumber = "";
                    string ClassName  = "";
                    string StudSeatNo = "";
                    if (_studDataDict.ContainsKey(studID))
                    {
                        studName   = _studDataDict[studID].Name;
                        StudNumber = _studDataDict[studID].StudentNumber;
                        if (_studDataDict[studID].Class != null)
                        {
                            ClassName = _studDataDict[studID].Class.Name;
                        }

                        if (_studDataDict[studID].SeatNo.HasValue)
                        {
                            StudSeatNo = _studDataDict[studID].SeatNo.Value.ToString();
                        }
                    }

                    List <UDT_CounselStudentInterviewRecordDef> studInterviewList = (from da in dataList where da.StudentID == sid orderby da.InterviewDate select da).ToList();

                    foreach (UDT_CounselStudentInterviewRecordDef data in studInterviewList)
                    {
                        Dictionary <string, string> mapDict = new Dictionary <string, string>();
                        mapDict.Add("校名", SchoolName);
                        mapDict.Add("學生姓名", studName);
                        mapDict.Add("學號", StudNumber);
                        mapDict.Add("班級", ClassName);
                        if (intTeacherNameDict.ContainsKey(data.TeacherID))
                        {
                            mapDict.Add("晤談老師", intTeacherNameDict[data.TeacherID]);
                        }

                        mapDict.Add("晤談編號", data.InterviewNo);
                        mapDict.Add("座號", StudSeatNo);
                        mapDict.Add("晤談對象", data.IntervieweeType);
                        mapDict.Add("晤談方式", data.InterviewType);
                        if (data.InterviewDate.HasValue)
                        {
                            mapDict.Add("晤談日期", data.InterviewDate.Value.ToShortDateString());
                        }
                        mapDict.Add("時間", data.InterviewTime);
                        mapDict.Add("地點", data.Place);
                        mapDict.Add("參與人員", ParseUDTXML1(data.Attendees));
                        mapDict.Add("晤談事由", data.Cause);
                        mapDict.Add("輔導方式", ParseUDTXML1(data.CounselType));
                        mapDict.Add("輔導歸類", ParseUDTXML1(data.CounselTypeKind));
                        mapDict.Add("內容要點", data.ContentDigest);
                        mapDict.Add("記錄者姓名", data.AuthorName);
                        _dataDictList.Add(mapDict);
                    }
                }
                #endregion
            }

            if (_UserSelectType == SelectType.教師)
            {
                #region 教師
                List <int> sortStudIDList = new List <int>();
                // 透過教師取得所屬晤談紀錄
                List <UDT_CounselStudentInterviewRecordDef> InterviewRecorT = _UDTTransfer.GetCounselStudentInterviewRecordByTeacherIDList(_TeacherIDList);
                // 取得晤談紀錄內學生id
                List <string> InterviewRecorStudIDList = new List <string>();
                foreach (UDT_CounselStudentInterviewRecordDef data in InterviewRecorT)
                {
                    if (!sortStudIDList.Contains(data.StudentID))
                    {
                        sortStudIDList.Add(data.StudentID);
                    }

                    string key = data.StudentID.ToString();
                    if (!InterviewRecorStudIDList.Contains(key))
                    {
                        InterviewRecorStudIDList.Add(key);
                    }
                }
                // 取得學生資訊
                Dictionary <int, StudentRecord> InterviewRecorStudDict = new Dictionary <int, StudentRecord>();
                foreach (StudentRecord rec in Student.SelectByIDs(InterviewRecorStudIDList))
                {
                    int sid = int.Parse(rec.ID);
                    if (!InterviewRecorStudDict.ContainsKey(sid))
                    {
                        InterviewRecorStudDict.Add(sid, rec);
                    }
                }

                // 轉換教師ID int
                List <int> intTeacherIDList = new List <int>();
                foreach (string tid in _TeacherIDList)
                {
                    intTeacherIDList.Add(int.Parse(tid));
                }

                sortStudIDList = Utility.SortStudentID2(sortStudIDList);
                // 組資料
                foreach (int id in intTeacherIDList)
                {
                    List <UDT_CounselStudentInterviewRecordDef> dataTT = (from data in InterviewRecorT where data.TeacherID == id orderby data.StudentID, data.InterviewDate select data).ToList();
                    // 依班級順序、班級名稱、學生座號排序後加入
                    List <UDT_CounselStudentInterviewRecordDef> dataT = new List <UDT_CounselStudentInterviewRecordDef>();
                    foreach (int ssid in sortStudIDList)
                    {
                        foreach (UDT_CounselStudentInterviewRecordDef data in dataTT.Where(x => x.StudentID == ssid))
                        {
                            dataT.Add(data);
                        }
                    }

                    foreach (UDT_CounselStudentInterviewRecordDef data in dataT)
                    {
                        string studName   = "";
                        string StudNumber = "";
                        string ClassName  = "";
                        string StudSeatNo = "";
                        if (InterviewRecorStudDict.ContainsKey(data.StudentID))
                        {
                            studName   = InterviewRecorStudDict[data.StudentID].Name;
                            StudNumber = InterviewRecorStudDict[data.StudentID].StudentNumber;
                            if (InterviewRecorStudDict[data.StudentID].Class != null)
                            {
                                ClassName = InterviewRecorStudDict[data.StudentID].Class.Name;
                            }

                            if (InterviewRecorStudDict[data.StudentID].SeatNo.HasValue)
                            {
                                StudSeatNo = InterviewRecorStudDict[data.StudentID].SeatNo.Value.ToString();
                            }
                        }

                        Dictionary <string, string> mapDict = new Dictionary <string, string>();
                        mapDict.Add("校名", SchoolName);
                        mapDict.Add("學生姓名", studName);
                        mapDict.Add("學號", StudNumber);
                        mapDict.Add("班級", ClassName);
                        if (intTeacherNameDict.ContainsKey(data.TeacherID))
                        {
                            mapDict.Add("晤談老師", intTeacherNameDict[data.TeacherID]);
                        }

                        mapDict.Add("晤談編號", data.InterviewNo);
                        mapDict.Add("座號", StudSeatNo);
                        mapDict.Add("晤談對象", data.IntervieweeType);
                        mapDict.Add("晤談方式", data.InterviewType);
                        if (data.InterviewDate.HasValue)
                        {
                            mapDict.Add("晤談日期", data.InterviewDate.Value.ToShortDateString());
                        }
                        mapDict.Add("時間", data.InterviewTime);
                        mapDict.Add("地點", data.Place);
                        mapDict.Add("參與人員", ParseUDTXML1(data.Attendees));
                        mapDict.Add("晤談事由", data.Cause);
                        mapDict.Add("輔導方式", ParseUDTXML1(data.CounselType));
                        mapDict.Add("輔導歸類", ParseUDTXML1(data.CounselTypeKind));
                        mapDict.Add("內容要點", data.ContentDigest);
                        mapDict.Add("記錄者姓名", data.AuthorName);
                        _dataDictList.Add(mapDict);
                    }
                }
                #endregion
            }

            // word 資料合併
            Document doc = new Document();
            doc.Sections.Clear();

            // 比對欄位名稱放值
            List <string> mapFieldName = new List <string>();
            mapFieldName.Add("校名");
            mapFieldName.Add("學生姓名");
            mapFieldName.Add("學號");
            mapFieldName.Add("班級");
            mapFieldName.Add("晤談老師");
            mapFieldName.Add("晤談編號");
            mapFieldName.Add("座號");
            mapFieldName.Add("晤談對象");
            mapFieldName.Add("晤談方式");
            mapFieldName.Add("晤談日期");
            mapFieldName.Add("時間");
            mapFieldName.Add("地點");
            mapFieldName.Add("參與人員");
            mapFieldName.Add("晤談事由");
            mapFieldName.Add("輔導方式");
            mapFieldName.Add("輔導歸類");
            mapFieldName.Add("內容要點");
            mapFieldName.Add("記錄者姓名");

            //取得範本樣式

            MemoryStream _template;
            if (!是否使用範本)
            {
                _template = new MemoryStream(Properties.Resources.學生晤談記錄表_新範本);
            }
            else
            {
                ConfigData cd     = K12.Data.School.Configuration[_ReportName];
                XmlElement config = cd.GetXml("XmlData", null);
                if (config != null)
                {
                    string templateBase64 = config.InnerText;
                    byte[] _buffer        = Convert.FromBase64String(templateBase64);
                    _template = new MemoryStream(_buffer);
                }
                else
                {
                    _template = new MemoryStream(Properties.Resources.學生晤談記錄表_新範本);
                }
            }


            foreach (Dictionary <string, string> data in _dataDictList)
            {
                DataTable dt = new DataTable();
                // 建立欄位名稱
                foreach (string name in mapFieldName)
                {
                    dt.Columns.Add(name, typeof(string));
                }

                DataRow dr = dt.NewRow();
                foreach (string name in mapFieldName)
                {
                    if (data.ContainsKey(name))
                    {
                        dr[name] = data[name];
                    }
                }
                dt.Rows.Add(dr);

                Document docTemplate = new Document(_template);

                docTemplate.MailMerge.FieldMergingCallback = new InsertDocumentAtMailMergeHandler();
                //已過時,改用Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs
                //docTemplate.MailMerge.RemoveEmptyParagraphs = true;

                docTemplate.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs;
                docTemplate.MailMerge.Execute(dt);
                docTemplate.MailMerge.DeleteFields();
                doc.Sections.Add(doc.ImportNode(docTemplate.Sections[0], true));
            }
            e.Result = doc;
        }