Пример #1
0
        /// <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();
            }
        }