private static void Import(int time) { string curDir = Environment.CurrentDirectory; string fileUrl = Path.Combine(curDir, "files", "CarImport.xlsx"); Stopwatch sw = new Stopwatch(); sw.Start(); var rows = ExcelImportService.Validate <ImportCar>(fileUrl, DBExist); Console.WriteLine($"------------第{time}次导入,处理{rows.Where(e=>e.IsValid).Count()}条数据------------"); sw.Stop(); Console.WriteLine($"Exel读取以及校验耗时:{sw.ElapsedMilliseconds}"); List <ImportCar> list = new List <ImportCar>(); sw.Restart(); foreach (var item in rows.Where(e => e.IsValid)) { //反射转换 - 5000条 6秒 item.ConvertByRefelection <ImportCar>(); } sw.Stop(); Console.WriteLine($"直接反射转换耗时:{sw.ElapsedMilliseconds}"); sw.Restart(); foreach (var item in rows.Where(e => e.IsValid)) { //反射转换 - 5000条 6秒 item.Convert <ImportCar>(); } sw.Stop(); Console.WriteLine($"反射+委托转换耗时:{sw.ElapsedMilliseconds}"); sw.Restart(); foreach (var item in rows.Where(e => e.IsValid)) { //Expression + 缓存转换 - 5000条3.5秒 list.Add(ExpressionMapper.FastConvert <ImportCar>(item)); } sw.Stop(); Console.WriteLine($"表达式树转换耗时:{sw.ElapsedMilliseconds}"); sw.Restart(); foreach (var item in rows.Where(e => e.IsValid)) { //Expression + 缓存转换 - 5000条3.5秒 list.Add(HardCode(item)); } sw.Stop(); Console.WriteLine($"硬编码转换耗时:{sw.ElapsedMilliseconds}"); Console.WriteLine("\r\n"); }
/// <summary> /// 通过表达式树,将单元行集合快速转换为指定类型集合 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="rows">单元行集合</param> private static IEnumerable <T> ConvertByExpressTree <T>(this IEnumerable <IRow> rows) { if (rows == null || !rows.Any()) { return(null); } Func <IList <ICell>, T> func = GetFunc <T>(rows.ToList()[0]); var list = new List <T>(); rows.ToList().ForEach(row => { var item = ExpressionMapper.FastConvert(row, func); list.Add(item); }); return(list); }