/// <summary> ///文件上传 /// </summary> /// <returns></returns> public IActionResult UpLoadFile(IFormFile file) { // 1.1存放Excel文件到本地服务器 string filePath = ExcelHelper.SaveExcelFile(file); // 保存文件并获取文件路径 //// 单元格抬头 // Description 属性对应抬头文字 var cellheader = ReflectionHelp.CellHeader <USER_INFO>(); // 1.2解析文件,存放到一个List集合里 StringBuilder errorMsg = new StringBuilder(); List <USER_INFO> enlist = ExcelHelper.ExcelToEntityList <USER_INFO>(cellheader, filePath, out errorMsg); // TODO:其他检测 bool isSuccess = false; if (errorMsg.Length == 0) { isSuccess = true; // 若错误信息成都为空,表示无错误信息 } var rs = new { success = isSuccess, msg = errorMsg.ToString(), data = enlist }; return(Json(rs)); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> public static void GetProAndFieldName <T>(T t) where T : Gastriloquist, ICollectFees { ReflectionHelp.GetFieldNameValue(t); ReflectionHelp.GetProNameValue(t); Gastriloquist gas = (Gastriloquist)t; gas.Start(); gas.StartSpeech(); gas.DogBark(); gas.PersonSpeak(); gas.WindSound(); gas.EndSpeech(); ((ICollectFees)t).GetCollectFees(); }
void init(FieldInfo field) { if (field == null) { throw new ArgumentException("Argument: field is null"); } var name = ReflectionHelper.GetMemberSignName(field); var type = field.DeclaringType; #if DEBUG1 ModuleBuilder moduleBuilder = ReflectionHelp.EmitDebug(); ISymbolDocumentWriter doc = moduleBuilder.DefineDocument(@"d:\IL的cs源代码片断在这里.txt", Guid.Empty, Guid.Empty, Guid.Empty); //要定义源代码位置,这个文档不需要全部源代码,只需要你想调试的il源代码翻译就可以了 DynamicMethod method = new DynamicMethod(name + "_get", ReflectionHelp.ObjectType, new Type[] { ReflectionHelp.ObjectType }, moduleBuilder); #else DynamicMethod method = new DynamicMethod(name + "_get", ReflectionHelper.ObjectType, new Type[] { ReflectionHelper.ObjectType }); #endif var il = method.GetILGenerator(); ReflectionHelper.ILLdarg(il, 0); ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type); il.Emit(OpCodes.Ldfld, field); ReflectionHelper.ILCastclass(il, field.FieldType, ReflectionHelper.ObjectType); #if DEBUG1 il.MarkSequencePoint(doc, 1, 0, 1, 100);//DynamicMethod不支持 #endif il.Emit(OpCodes.Ret); FieldGet = (Func <object, object>)method.CreateDelegate(typeof(Func <object, object>)); method = new DynamicMethod(name + "_set", ReflectionHelper.VoidType, new Type[] { ReflectionHelper.ObjectType, ReflectionHelper.ObjectType }); il = method.GetILGenerator(); ReflectionHelper.ILLdarg(il, 0); ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, type); ReflectionHelper.ILLdarg(il, 1); ReflectionHelper.ILCastclass(il, ReflectionHelper.ObjectType, field.FieldType); il.Emit(OpCodes.Stfld, field); il.Emit(OpCodes.Ret); FieldSet = (Action <object, object>)method.CreateDelegate(typeof(Action <object, object>)); }
/// <summary> /// 实体导出成Excel对象 /// </summary> public IActionResult EntityToExcel() { List <USER_INFO> list = new List <USER_INFO>() { new USER_INFO { USER_NO = "xxx", USER_NAME = "xx", Email = DateTime.Now }, new USER_INFO { USER_NO = "xxx", USER_NAME = "xx", Email = DateTime.Now }, new USER_INFO { USER_NO = "xxxx", USER_NAME = "xx", Email = DateTime.Now } }; // 2.设置单元格抬头 // key:实体对象属性名称,可通过反射获取值 // value:Excel列的名称 var cellheader = ReflectionHelp.CellHeader <USER_INFO>(); var result = ExcelHelper.EntityListToExcel2003(cellheader, list, "个人信息"); return(null); }