public SharedResultVM Export(QueryDataInputVM ivm, string title = "export") { var vm = new SharedResultVM(); //虚拟路径 string vpath = GlobalTo.GetValue("StaticResource:TmpDir"); //物理路径 var ppath = PathTo.Combine(GlobalTo.WebRootPath, vpath); if (!Directory.Exists(ppath)) { Directory.CreateDirectory(ppath); } //文件名 string filename = $"{title.Replace(" ", "").Trim()}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx"; //导出的表数据 var dtReport = new DataTable(); try { switch (ivm.TableName?.ToLower()) { default: vm.Set(SharedEnum.RTag.invalid); break; //角色 case "sysrole": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysRole(ivm)); } break; //用户 case "sysuser": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysUser(ivm)); } break; //日志 case "syslog": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysLog(ivm)); } break; //字典 case "sysdictionary": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysDictionary(ivm)); } break; } Console.WriteLine($"Export table rows : {dtReport.Rows.Count}"); if (vm.Msg != SharedEnum.RTag.invalid.ToString()) { //生成 if (NpoiTo.DataTableToExcel(dtReport, PathTo.Combine(ppath, filename))) { vm.Data = PathTo.Combine(vpath, filename); //生成的Excel继续操作 ExportService.ExcelDraw(PathTo.Combine(ppath, filename), ivm); vm.Set(SharedEnum.RTag.success); } else { vm.Set(SharedEnum.RTag.fail); } } } catch (Exception ex) { vm.Set(ex); } return(vm); }
public ActionResultVM Export(QueryDataInputVM ivm, string title = "export") { var vm = new ActionResultVM(); //文件路径 string path = "/upload/temp/"; var vpath = GlobalTo.WebRootPath + path; if (!Directory.Exists(vpath)) { Directory.CreateDirectory(vpath); } //文件名 string filename = title.Replace(" ", "").Trim() + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx"; //导出的表数据 var dtReport = new DataTable(); try { switch (ivm.TableName?.ToLower()) { default: vm.Set(ARTag.invalid); break; //角色 case "sysrole": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysRole(ivm)); } break; //用户 case "sysuser": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysUser(ivm)); } break; //日志 case "syslog": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysLog(ivm)); } break; //字典 case "sysdictionary": { using var ctl = new SettingController(db); dtReport = ExportService.ModelsMapping(ivm, ctl.QuerySysDictionary(ivm)); } break; } if (vm.Msg != ARTag.invalid.ToString()) { //生成 if (Fast.NpoiTo.DataTableToExcel(dtReport, vpath + filename)) { vm.Data = path + filename; //生成的Excel继续操作 ExportService.ExcelDraw(vpath + filename, ivm); vm.Set(ARTag.success); } else { vm.Set(ARTag.fail); } } } catch (Exception ex) { Console.WriteLine(ex.Message); vm.Set(ex); } return(vm); }