public static object GetObject(System.Data.DataRow row, System.Data.DataColumnCollection colums, object obj, string entityName) { for (var i = 0; i < colums.Count; i++) { PropertyInfo p = obj.GetType().GetProperty(colums[i].ColumnName.ToString()); string propValue = row[colums[i].ColumnName.ToString()].ToString(); switch (p.PropertyType.Name.ToLower()) { case "string": p.SetValue(obj, propValue, null); break; case "nullable`1": if (string.IsNullOrEmpty(propValue)) { p.SetValue(obj, null, null); break; } if (p.PropertyType.FullName.ToLower().Contains("system.int32")) { p.SetValue(obj, int.Parse(propValue), null); } if (p.PropertyType.FullName.ToLower().Contains("system.boolean")) { propValue = propValue.Trim() == "30" ? "true" : "false"; //zjc.2016.5.31 //在新增的时候值为1报错。只能录入TRUE,在修改时。TRUE值又报错。所以在新增时遇到1改为true p.SetValue(obj, bool.Parse(propValue), null); } if (p.PropertyType.FullName.ToLower().Contains("system.decimal")) { p.SetValue(obj, decimal.Parse(propValue), null); } if (p.PropertyType.FullName.ToLower().Contains("system.guid")) { ///TODO:firefox中replace(/'/g, "")未能执行成功,临时代码依靠c#解决 p.SetValue(obj, Guid.Parse(propValue.Replace("'", "")), null); } if (p.PropertyType.FullName.ToLower().Contains("system.datetime")) { p.SetValue(obj, DateTime.Parse(propValue), null); } break; case "int32": p.SetValue(obj, string.IsNullOrEmpty(propValue) ? 0 : int.Parse(propValue), null); break; case "bool": propValue = propValue.Trim() == "30" ? "true" : "false"; p.SetValue(obj, bool.Parse(propValue), null); break; case "guid": p.SetValue(obj, string.IsNullOrEmpty(propValue) ? Guid.Empty : Guid.Parse(propValue.Replace("'", "")), null); break; case "datetime": p.SetValue(obj, string.IsNullOrEmpty(propValue) ? DateTime.Parse("1900-01-01") : DateTime.Parse(propValue), null); break; } } if (SYSHandler.ValidateFields(obj, entityName) == false) { throw new Exception(); } return(obj); }
/// <summary> /// 供货计划相关EXCEL导出 /// </summary> /// <param name="context"></param> /// <returns></returns> public string SupplyPlanExportExcel(HttpContext context) { ///ENTITY_NAME string entityName = context.Request["ENTITY_NAME"]; if (entityName.IndexOf("_") != -1) { entityName = entityName.Substring(0, entityName.IndexOf("_")); } if (HttpCommon.IsNullOrEmptyOrUndefined(entityName)) { throw new Exception("MC:1x00000030");///参数获取错误 } ///fileName string fileName = entityName + DateTime.Now.Ticks + ".xlsx"; ///filePath string filePath = HttpContext.Current.Server.MapPath("/TEMP/EXPORTFILES"); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } ///sort string orderText = string.Empty; if (!HttpCommon.IsNullOrEmptyOrUndefined(context.Request["sort"])) { orderText += context.Request["sort"]; if (!orderText.Contains("_")) { orderText = DataCommon.GetFieldName(orderText.Trim()); } } ///order if (!HttpCommon.IsNullOrEmptyOrUndefined(context.Request["order"])) { orderText += " " + context.Request["order"]; } ///FILTER string textWhere = context.Request["FILTER"]; if (HttpCommon.IsNullOrEmptyOrUndefined(textWhere)) { textWhere = string.Empty; } if (textWhere.Trim().ToUpper() == "AND") { textWhere = string.Empty; } ///创建标题行 List <EntityFieldInfo> entityFieldInfos = new List <EntityFieldInfo>(); switch (entityName) { ///供货计划 case "SupplyPlan": entityFieldInfos = GetSupplyPlanFields(textWhere, entityName) .OrderBy(d => d.DisplayOrder.GetValueOrDefault()) .OrderBy(d => d.ExportExcelOrder.GetValueOrDefault()).ToList(); break; ///缺件检查 case "LackOfInspection": entityFieldInfos = GetSupplyPlanCheckFields(textWhere, entityName) .OrderBy(d => d.DisplayOrder.GetValueOrDefault()) .OrderBy(d => d.ExportExcelOrder.GetValueOrDefault()).ToList(); break; } if (entityFieldInfos.Count == 0) { throw new Exception("MC:1x00000033");///未设置导出内容 } ///CODE_ITEM下拉菜单集合 Dictionary <string, Dictionary <string, string> > dicDropdowns = new Dictionary <string, Dictionary <string, string> >(); Dictionary <string, string> columnNames = SYSHandler.GetExcelColumnNames(entityFieldInfos, ref dicDropdowns); ///page int pageIndex = 1; ///rows int maxRow = int.MaxValue; int dataTotal; ///获取数据 DataTable dataTable = new DataTable(); switch (entityName) { ///供货计划 case "SupplyPlan": dataTable = GetSupplyPlanData(textWhere, orderText, pageIndex, maxRow, out dataTotal); break; ///缺件检查 case "LackOfInspection": dataTable = GetLackOfInspectionData(textWhere, orderText, pageIndex, maxRow, out dataTotal); break; } if (dataTable.Rows.Count > 65535) { throw new Exception("MC:1x00000036");///导出数据不能多于65535条 } ///NPOI NpoiHelper.TableToExcel(dataTable, columnNames, dicDropdowns, entityName, filePath + @"\" + fileName); /// return("../TEMP/EXPORTFILES/" + fileName); }