public ExportTable Export() { // 取得匯出規則描述 XmlElement descElement = TeacherBulkProcess.GetExportDescription(); IFieldFormater fieldFormater = new BaseFieldFormater(); IResponseFormater responseFormater = new ResponseFormater(); FieldCollection fieldCollection = fieldFormater.Format(descElement); ExportFieldCollection exportFields = responseFormater.Format(descElement); fieldCollection = FieldUtil.Match(fieldCollection, _selectFields); exportFields = FieldUtil.Match(exportFields, _selectFields); IRequestGenerator reqGenerator = new ExportStudentRequestGenerator(); reqGenerator.SetSelectedFields(_selectFields); ICondition condition = new BaseCondition("ID", "-1"); reqGenerator.AddCondition(condition); foreach (string id in _conditions) { ICondition condition2 = new BaseCondition("ID", id); reqGenerator.AddCondition(condition2); } DSRequest request = reqGenerator.Generate(); DSResponse response = TeacherBulkProcess.GetExportList(request); ExportTable table = new ExportTable(); foreach (ExportField field in exportFields) { table.AddColumn(field); } foreach (XmlElement record in response.GetContent().GetElements("Teacher")) { ExportRow row = table.AddRow(); foreach (ExportField column in table.Columns) { int columnIndex = column.ColumnIndex; ExportCell cell = row.Cells[columnIndex]; XmlNode cellNode = record.SelectSingleNode(column.XPath); if (cellNode != null) { cell.Value = cellNode.InnerText; } } } return(table); }
public void SetSource(ExportTable source) { Workbook book = new Workbook(); Worksheet sheet = book.Worksheets[0]; foreach (ExportField field in source.Columns) { Cell wcell = sheet.Cells[0, field.ColumnIndex]; wcell.PutValue(field.DisplayText); } foreach (ExportRow row in source.Rows) { foreach (ExportField field in source.Columns) { Cell wcell = sheet.Cells[row.Index + 1, field.ColumnIndex]; ExportCell ecell = row.Cells[field.ColumnIndex]; string value = ecell.Value; IConverter converter = ConverterFactory.GetInstance(field.Converter); value = converter.Convert(value); IDataType dataType = DataTypeFactory.GetInstance(field.DataType); dataType.SetValue(value); if (!dataType.IsValidDataType) { wcell.PutValue(value, false); } else { switch (field.DataType.ToLower()) { case "integer": //wcell.PutValue(int.Parse(value)); wcell.PutValue(value, true); break; case "double": //wcell.PutValue(double.Parse(value)); wcell.PutValue(value, true); break; case "datetime": //int styleIndex = book.Styles.Add(); //Style style = book.Styles[styleIndex]; //style.Number = 14; //style.Custom = "yyyy/MM/dd;@"; //wcell.Style.Copy(style); DateTime dt = Convert.ToDateTime(dataType.GetTypeValue()); wcell.PutValue(dt.ToShortDateString(), false); //wcell.PutValue(DateTime.Parse(value)); break; case "object": wcell.PutValue((object)value); break; default: wcell.PutValue(value, false); break; } } } } _book = book; }
public ExportTable Export() { // 取得縣市對照表 XmlElement schoolLocationList = Config.GetSchoolLocationList().GetContent().BaseElement; // 取得匯出規則描述 XmlElement descElement = StudentBulkProcess.GetExportDescription(); IFieldFormater fieldFormater = new BaseFieldFormater(); IResponseFormater responseFormater = new ResponseFormater(); FieldCollection fieldCollection = fieldFormater.Format(descElement); ExportFieldCollection exportFields = responseFormater.Format(descElement); fieldCollection = FieldUtil.Match(fieldCollection, _selectFields); exportFields = FieldUtil.Match(exportFields, _selectFields); //// 有選狀態時加入 //if (_selectFields.FindByDisplayText("狀態") != null) //{ // fieldCollection.Add(_selectFields.FindByDisplayText("狀態")); // ExportField ex = new ExportField(); // ex.DisplayText = "狀態"; // ex.RequestName = "StudentStatus"; // ex.ColumnIndex = exportFields.Length; // ex.DataType = ""; // ex.XPath = ""; // exportFields.Add(ex); //} IRequestGenerator reqGenerator = new ExportStudentRequestGenerator(); _selectFieldsID = new FieldCollection(); foreach (Field fd in _selectFields) { _selectFieldsID.Add(fd); } if (_selectFieldsID.Find("StudentID") == null) { Field fd1 = new Field(); fd1.FieldName = "StudentID"; fd1.DisplayText = "學生系統編號"; _selectFieldsID.Add(fd1); } reqGenerator.SetSelectedFields(_selectFieldsID); // 預設找-1, 不然會傳回所有學生 ICondition condition = new BaseCondition("ID", "-1"); reqGenerator.AddCondition(condition); foreach (string id in _conditions) { ICondition condition2 = new BaseCondition("ID", id); reqGenerator.AddCondition(condition2); } reqGenerator.AddOrder(new Order("GradeYear")); reqGenerator.AddOrder(new Order("Department")); reqGenerator.AddOrder(new Order("RefClassID")); reqGenerator.AddOrder(new Order("SeatNo")); DSRequest request = reqGenerator.Generate(); DSResponse response = QueryStudent.GetExportList(request); ExportTable table = new ExportTable(); foreach (ExportField field in exportFields) { table.AddColumn(field); } //// 取得學生狀態 //Dictionary<string, string> StudStatusDic = new Dictionary<string, string>(); //foreach (JHSchool.Data.JHStudentRecord stud in JHSchool.Data.JHStudent.SelectByIDs(K12.Presentation.NLDPanels.Student.SelectedSource )) // StudStatusDic.Add(stud.ID, stud.Status.ToString()); foreach (XmlElement record in response.GetContent().GetElements("Student")) { ExportRow row = table.AddRow(); foreach (ExportField column in table.Columns) { int columnIndex = column.ColumnIndex; ExportCell cell = row.Cells[columnIndex]; XmlNode cellNode = record.SelectSingleNode(column.XPath); //if(column.DisplayText !="狀態") // cellNode = record.SelectSingleNode(column.XPath); // CustodianOtherInfo/CustodianOtherInfo[1]/EducationDegree[1] #region 這段程式是處理匯入/匯出程式不一致問題 if (column.XPath.StartsWith("CustodianOtherInfo/Custodian")) { if (cellNode == null) { string x = column.XPath.Replace("CustodianOtherInfo/Custodian", "CustodianOtherInfo/CustodianOtherInfo"); cellNode = record.SelectSingleNode(x); if (cellNode == null) { x = column.XPath.Replace("CustodianOtherInfo/CustodianOtherInfo", "CustodianOtherInfo/Custodian"); cellNode = record.SelectSingleNode(x); } } } if (column.XPath.StartsWith("FatherOtherInfo/Father")) { if (cellNode == null) { string x = column.XPath.Replace("FatherOtherInfo/Father", "FatherOtherInfo/FatherOtherInfo"); cellNode = record.SelectSingleNode(x); if (cellNode == null) { x = column.XPath.Replace("FatherOtherInfo/FatherOtherInfo", "FatherOtherInfo/Father"); cellNode = record.SelectSingleNode(x); } } } if (column.XPath.StartsWith("MotherOtherInfo/Mother")) { if (cellNode == null) { string x = column.XPath.Replace("MotherOtherInfo/Mother", "MotherOtherInfo/MotherOtherInfo"); cellNode = record.SelectSingleNode(x); if (cellNode == null) { x = column.XPath.Replace("MotherOtherInfo/MotherOtherInfo", "MotherOtherInfo/Mother"); cellNode = record.SelectSingleNode(x); } } } #endregion if (cellNode != null) { if (column.FieldName == "GraduateSchoolLocationCode") { cell.Value = GetCounty(schoolLocationList, cellNode.InnerText); } else if (column.FieldName == "DeptName") //處理科別繼承問題。 { //這個欄位的資料一定會被回傳,因為設定了 Mandatory 屬性。 XmlNode selfDept = record.SelectSingleNode("SelfDeptName"); if (string.IsNullOrEmpty(selfDept.InnerText)) { cell.Value = cellNode.InnerText; } else { cell.Value = selfDept.InnerText; } } else if (column.FieldName == "Status") { cell.Value = GetStudStatusStr(cellNode.InnerText); } else { cell.Value = cellNode.InnerText; } } //if (column.DisplayText == "狀態")//record.SelectSingleNode("StudentID")!=null ) //{ // // 學生狀態 // if (StudStatusDic.ContainsKey(record.SelectSingleNode("StudentID").InnerText)) // cell.Value = StudStatusDic[record.SelectSingleNode("StudentID").InnerText]; //} } } return(table); }