void _bgWorkerLoad_DoWork(object sender, DoWorkEventArgs e)
        {
            // 取得學生類別
            _StudentTagList = QueryTransfer.GetStudentTagList();

            _SelectMappingDict1 = tool.GetExcessCreditsBase1();
            _SelectMappingDict2 = tool.GetExcessCreditsBase2();
            _SelectMappingDict3 = tool.GetExcessCreditsBase3();

            // 取得設定檔
            _UDTConfigList = UDTTransfer.UDTConfigSelectByName(conf_name);
            if (_UDTConfigList.Count > 0)
            {
                UDTConfig cd = _UDTConfigList[0];
                if (!string.IsNullOrWhiteSpace(cd.Data))
                {
                    try
                    {
                        _ConfigXML = XElement.Parse(cd.Data);
                    }
                    catch
                    {
                    }
                }
            }
        }
        void _bgWorkerRun_DoWork(object sender, DoWorkEventArgs e)
        {
            // 學生基本資料與相關資料
            _AddressRecDict.Clear();
            _ParentDict.Clear();
            _PhoneDict.Clear();
            _StudentDict.Clear();
            _dtTable.Clear();
            _ClassNameDict.Clear();

            // 學生畫面上所選,再排序過
            _SelectStudentList = QueryTransfer.StudentSortByClassSeatNo(K12.Presentation.NLDPanels.Student.SelectedSource);

            // 三,九年級,一般狀態學生
            _StudGradeList = QueryTransfer.GetStudeGrade3List();

            // 學生,來自學生所選
            if (_isStudent)
            {
                foreach (StudentRecord stud in Student.SelectByIDs(_SelectStudentList))
                {
                    _StudentDict.Add(stud.ID, stud);
                }
            }
            else
            {
                foreach (StudentRecord stud in Student.SelectByIDs(_StudGradeList))
                {
                    _StudentDict.Add(stud.ID, stud);
                }
            }

            // 班級
            foreach (ClassRecord rec in Class.SelectAll())
            {
                _ClassNameDict.Add(rec.ID, rec.Name.Substring(1, rec.Name.Length - 1));
            }

            // 地址
            foreach (AddressRecord rec in Address.SelectByStudentIDs(_StudGradeList))
            {
                _AddressRecDict.Add(rec.RefStudentID, rec);
            }

            // 父母
            foreach (ParentRecord rec in K12.Data.Parent.SelectByStudentIDs(_StudGradeList))
            {
                _ParentDict.Add(rec.RefStudentID, rec);
            }

            // 電話
            foreach (PhoneRecord rec in Phone.SelectByStudentIDs(_StudGradeList))
            {
                _PhoneDict.Add(rec.RefStudentID, rec);
            }

            // 學生類別
            _StudTagDict = QueryTransfer.GetStduentTagDict();

            // 填入DataColumn name 資料
            foreach (string name in tool.GetDataTableColumnsName())
            {
                _dtTable.Columns.Add(name);
            }

            List <string> tmptagList1 = new List <string>();
            List <string> tmptagList2 = new List <string>();

            List <string> exportStudentIDList = new List <string>();

            if (_isStudent)
            {
                exportStudentIDList = _SelectStudentList;
            }
            else
            {
                exportStudentIDList = _StudGradeList;
            }

            // 填入 DataTable
            foreach (string id in exportStudentIDList)
            {
                // 地區代碼、集報單位代碼、序號、學號、班級、座號、學生姓名、身分證統一編號、性別、出生年、出生月、出生日、
                // 畢業學校代碼、畢業年、畢肄業、學生身分、身心障礙、就學區、低收入戶、中低收入戶、失業勞工、資料授權、
                // 家長姓名、室內電話、行動電話、郵遞區號、地址

                DataRow dr = _dtTable.NewRow();

                if (_StudentDict.ContainsKey(id))
                {
                    dr["地區代碼"] = "";
                    if (_SelectMappingDict1.ContainsKey(strType1))
                    {
                        dr["地區代碼"] = _SelectMappingDict1[strType1];
                    }

                    dr["集報單位代碼"] = K12.Data.School.Code;

                    dr["學號"] = _StudentDict[id].StudentNumber;
                    dr["班級"] = "";
                    if (_ClassNameDict.ContainsKey(_StudentDict[id].RefClassID))
                    {
                        dr["班級"] = _ClassNameDict[_StudentDict[id].RefClassID].PadLeft(2, '0');
                    }

                    dr["座號"] = "";
                    if (_StudentDict[id].SeatNo.HasValue)
                    {
                        dr["座號"] = _StudentDict[id].SeatNo.Value.ToString().PadLeft(2, '0');
                    }

                    dr["學生姓名"]    = _StudentDict[id].Name;
                    dr["身分證統一編號"] = _StudentDict[id].IDNumber.ToUpper();

                    dr["性別"] = "";
                    if (_StudentDict[id].Gender == "男")
                    {
                        dr["性別"] = "1";
                    }

                    if (_StudentDict[id].Gender == "女")
                    {
                        dr["性別"] = "2";
                    }

                    dr["出生年"] = dr["出生月"] = dr["出生日"] = "";
                    if (_StudentDict[id].Birthday.HasValue)
                    {
                        dr["出生年"] = (_StudentDict[id].Birthday.Value.Year - 1911).ToString().PadLeft(3, '0');
                        dr["出生月"] = (_StudentDict[id].Birthday.Value.Month).ToString().PadLeft(2, '0');
                        dr["出生日"] = (_StudentDict[id].Birthday.Value.Day).ToString().PadLeft(2, '0');
                    }

                    dr["畢業學校代碼"] = K12.Data.School.Code;

                    dr["畢業年"]   = integerInput1.Value;
                    dr["畢肄業"]   = "1";
                    dr["學生身分"]  = "0";
                    dr["身心障礙"]  = "0";
                    dr["低收入戶"]  = "0";
                    dr["中低收入戶"] = "0";
                    dr["失業勞工"]  = "0";

                    tmptagList1.Clear();
                    tmptagList2.Clear();

                    if (_StudTagDict.ContainsKey(id))
                    {
                        foreach (string name in _StudTagDict[id])
                        {
                            if (_strStudTagMappingDict1.ContainsKey(name))
                            {
                                tmptagList1.Add(_strStudTagMappingDict1[name]);
                            }

                            if (_strStudTagMappingDict2.ContainsKey(name))
                            {
                                tmptagList2.Add(_strStudTagMappingDict2[name]);
                            }
                        }

                        if (tmptagList1.Count > 0)
                        {
                            dr["學生身分"] = string.Join(",", tmptagList1.ToArray());
                        }

                        if (tmptagList2.Count > 0)
                        {
                            dr["身心障礙"] = string.Join(",", tmptagList2.ToArray());
                        }


                        if (_StudTagDict[id].Contains(strType2))
                        {
                            dr["低收入戶"] = "1";
                        }

                        if (_StudTagDict[id].Contains(strType3))
                        {
                            dr["中低收入戶"] = "1";
                        }

                        if (_StudTagDict[id].Contains(strType4))
                        {
                            dr["失業勞工"] = "1";
                        }
                    }

                    dr["資料授權"] = "1";

                    //室內電話,行動電話
                    dr["室內電話"] = "";
                    dr["行動電話"] = "";
                    if (_PhoneDict.ContainsKey(id))
                    {
                        if (strType6 == "戶籍電話")
                        {
                            dr["室內電話"] = tool.ParseTelStr(_PhoneDict[id].Permanent);
                        }
                        if (strType6 == "聯絡電話")
                        {
                            dr["室內電話"] = tool.ParseTelStr(_PhoneDict[id].Contact);
                        }
                        dr["行動電話"] = tool.ParseTelStr(_PhoneDict[id].Cell);
                    }

                    // 監護人
                    dr["家長姓名"] = "";
                    if (_ParentDict.ContainsKey(id))
                    {
                        if (strType7 == "父親")
                        {
                            dr["家長姓名"] = _ParentDict[id].FatherName;
                        }

                        if (strType7 == "母親")
                        {
                            dr["家長姓名"] = _ParentDict[id].MotherName;
                        }

                        if (strType7 == "監護人")
                        {
                            dr["家長姓名"] = _ParentDict[id].CustodianName;
                        }
                    }

                    // 通訊地址
                    dr["郵遞區號"] = "";
                    dr["地址"]   = "";
                    if (_AddressRecDict.ContainsKey(id))
                    {
                        if (strType5 == "聯絡")
                        {
                            dr["郵遞區號"] = _AddressRecDict[id].MailingZipCode;

                            dr["地址"] = _AddressRecDict[id].MailingCounty + _AddressRecDict[id].MailingTown + _AddressRecDict[id].MailingDistrict + _AddressRecDict[id].MailingArea + _AddressRecDict[id].MailingDetail;
                        }

                        if (strType5 == "戶籍")
                        {
                            dr["郵遞區號"] = _AddressRecDict[id].PermanentZipCode;
                            dr["地址"]   = _AddressRecDict[id].PermanentCounty + _AddressRecDict[id].PermanentTown + _AddressRecDict[id].PermanentDistrict + _AddressRecDict[id].PermanentArea + _AddressRecDict[id].PermanentDetail;
                        }
                    }
                }

                _dtTable.Rows.Add(dr);
            }

            // 取得樣板
            Workbook wb = new Workbook();

            wb.Open(new MemoryStream(Properties.Resources.基本資料樣板));

            // 取得樣板欄位索引
            Dictionary <string, int> ColNameDict = new Dictionary <string, int>();

            for (int idx = 0; idx <= wb.Worksheets[0].Cells.MaxDataColumn; idx++)
            {
                string name = wb.Worksheets[0].Cells[0, idx].StringValue;
                if (!ColNameDict.ContainsKey(name))
                {
                    ColNameDict.Add(name, idx);
                }
            }

            // 填入資料
            int rowIdx = 1;

            foreach (DataRow dr in _dtTable.Rows)
            {
                foreach (string name in tool.GetDataTableColumnsName())
                {
                    if (ColNameDict.ContainsKey(name))
                    {
                        wb.Worksheets[0].Cells[rowIdx, ColNameDict[name]].PutValue(dr[name].ToString());
                    }
                }

                rowIdx++;
            }

            e.Result = wb;
        }