/// <summary> /// Write datatable to a csv file /// </summary> /// <param name="dataTable">Data table</param> /// <param name="savePath">Save path,Will save file to the application root directory when the parameter value is null or empty</param> /// <param name="fileName">Without extension file name,Will use a random file name when the parameter value is null or empty</param> /// <param name="ignoreTitle">Whether ignore column name,Default is false</param> /// <returns>Return the file full path</returns> public static string WriteToCSVFile(this DataTable dataTable, string savePath = "", string fileName = "", bool ignoreTitle = false) { if (dataTable == null) { throw new ArgumentNullException(nameof(dataTable)); } if (string.IsNullOrWhiteSpace(fileName)) { fileName = GuidCodeHelper.GetGuidUniqueCode().ToLower(); } if (string.IsNullOrWhiteSpace(savePath)) { savePath = ApplicationManager.GetApplicationExecutableDirectory(); } if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string filePath = Path.Combine(savePath, $"{fileName}.csv"); using (StreamWriter sw = new StreamWriter(filePath, false)) { List <string> lineDatas = new List <string>(dataTable.Columns.Count); if (!ignoreTitle) { foreach (DataColumn col in dataTable.Columns) { lineDatas.Add(col.ColumnName); } string titles = string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, lineDatas); sw.WriteLine(titles); } int columnCount = dataTable.Columns.Count; foreach (DataRow row in dataTable.Rows) { lineDatas.Clear(); for (int i = 0; i < columnCount; i++) { lineDatas.Add(Convert.IsDBNull(row[i]) ? string.Empty : row[i].ToString()); } sw.WriteLine(string.Join(CultureInfo.CurrentCulture.TextInfo.ListSeparator, lineDatas)); } sw.Close(); } return(filePath); }
public ActionResult EditAuthority(EditAuthorityViewModel authority, long groupSysNo = 0) { if (IsPost) { SaveAuthorityCmdDto saveInfo = new SaveAuthorityCmdDto() { Authority = authority.MapTo <AuthorityCmdDto>() }; var result = authService.SaveAuthority(saveInfo); var ajaxResult = AjaxResult.CopyFromResult(result); ajaxResult.SuccessClose = true; return(Json(ajaxResult)); } else { if (authority.SysNo < 1) { if (groupSysNo > 0) { authority.Group = authService.GetAuthorityGroup(new AuthorityGroupFilterDto() { SysNos = new List <long>() { groupSysNo } })?.MapTo <EditAuthorityGroupViewModel>(); authority.Code = GuidCodeHelper.GetGuidUniqueCode().ToUpper(); } } else { AuthorityFilterDto filter = new AuthorityFilterDto() { SysNos = new List <long>() { authority.SysNo }, LoadGroup = true }; authority = authService.GetAuthority(filter).MapTo <EditAuthorityViewModel>(); } return(View(authority)); } }
public ActionResult EditPermission(PermissionViewModel perission, long groupId = 0) { if (IsPost) { SavePermissionDto savePermissionDto = new SavePermissionDto() { Permission = perission.MapTo <PermissionDto>() }; var result = permissionAppService.SavePermission(savePermissionDto); var ajaxResult = AjaxResult.CopyFromResult(result); ajaxResult.SuccessClose = true; return(Json(ajaxResult)); } else { if (perission.Id < 1) { if (groupId > 0) { perission.Group = permissionGroupAppService.GetPermissionGroup(new PermissionGroupFilterDto() { Ids = new List <long>() { groupId } })?.MapTo <PermissionGroupViewModel>(); perission.Code = GuidCodeHelper.GetGuidUniqueCode().ToUpper(); } } else { PermissionFilterDto filter = new PermissionFilterDto() { Ids = new List <long>() { perission.Id }, LoadGroup = true }; perission = permissionAppService.GetPermission(filter).MapTo <PermissionViewModel>(); } return(View(perission)); } }