/// <summary> /// 获取用户列表 /// </summary> /// <returns></returns> public JsonResult List(SearchUserListInputDto input) { //检查访问权限 CheckPermission(); using (ResponseResult <UserModel> result = new ResponseResult <UserModel>()) { WhereModel wheremodel = new WhereModel() { RoleId = Convert.ToInt32(input.RoleId), Province = input.Province, City = input.City, Region = input.Region, PhoneNumber = input.PhoneNumber, StartTime = Convert.ToDateTime(input.StartTime), EndTime = Convert.ToDateTime(input.EndTime), UserTypeId = input.UserTypeId }; long recordCount; var modelList = this._userService.GetUserList(input.pageIndex.Value, input.pageSize.Value, out recordCount, wheremodel); modelList.Select(m => { var addressNames = _areaRepository.GetAreaNamesBy(string.Format("{0}|{1}|{2}|{3}|{4}", m.Province, m.City, m.Region, m.Township, m.Village)); var addressNamesArray = addressNames.Split('|'); m.Province = addressNamesArray[0]; m.City = addressNamesArray[1]; m.Region = addressNamesArray[2]; m.Township = addressNamesArray[3]; m.Village = addressNamesArray[4]; return(m); }).Count(); var usersModel = new UserModel() { Pager = new PagedList <string>(new string[0], input.pageIndex.Value, input.pageSize.Value, (int)recordCount), PendingAuditList = modelList, Wheremodel = wheremodel }; result.IsSuccess = true; result.Entity = usersModel; result.TotalNums = recordCount; result.PageIndex = input.pageIndex.Value; result.PageSize = input.pageSize.Value; return(new JsonResultEx(result)); } }
public ActionResult ExportExcelWithUserList(SearchUserListInputDto input) { CheckPermission(GetLoginInfo().User.Id, CurrentUrl); RestSharp.RestClient client = new RestSharp.RestClient(); RestSharp.RestRequest request = new RestSharp.RestRequest(); client.BaseUrl = new Uri(GetCurrentUrl(this)); WhereModel wheremodel; wheremodel = new WhereModel() { RoleId = Convert.ToInt32(Request["RoleId"].DefaultIfEmpty("0")), Province = Request["Province"].DefaultIfEmpty("0"), City = Request["City"].DefaultIfEmpty("0"), Region = Request["Region"].DefaultIfEmpty("0"), PhoneNumber = Request["PhoneNumber"].DefaultIfEmpty(string.Empty), }; Dictionary <string, string> parameters; GetSearchParameters(input.pageIndex.Value, input.pageSize.Value, out wheremodel, out parameters); foreach (var para in parameters) { request.AddParameter(para.Key, para.Value.IsNullOrEmpty() ? null : para.Value); } if (request.Parameters.Count(p => p.Name == DataKey.UserId) == 0) { request.AddParameter(DataKey.UserId, GetLoginInfo().User.Id.ToString()); } var responseResult = client.ExecuteAsGet(request, "GET"); if (responseResult.Content == "no data") { return(Content("<script>alert('没有符合条件的数据可被导出!');history.go(-1)</script>")); } var contentDispositionHeader = responseResult.Headers.First(p => p.Name == "Content-Disposition").Value.ToString().Replace(" ", string.Empty); var attachFileName = contentDispositionHeader.Replace("attachment;filename=", string.Empty); return(File(responseResult.RawBytes, responseResult.ContentType, attachFileName)); }
public ActionResult ExportExcelWithUserList(SearchUserListInputDto input) { CheckPermission(); WhereModel wheremodel = new WhereModel() { RoleId = Convert.ToInt32(input.RoleId), Province = input.Province, City = input.City, Region = input.Region, PhoneNumber = input.PhoneNumber, StartTime = Convert.ToDateTime(input.StartTime), EndTime = Convert.ToDateTime(input.EndTime) }; long recordCount; var modelList = this._userService.GetUserList(input.pageIndex.Value, input.pageSize.Value, out recordCount, wheremodel); var usersModel = new UserModel() { Pager = new PagedList <string>(new string[0], input.pageIndex.Value, input.pageSize.Value, (int)recordCount), PendingAuditList = modelList, Wheremodel = wheremodel }; if (modelList != null && modelList.Count > 0) { var areaCodeDictionary = new Dictionary <string, string>(); modelList.Select(m => { if (ValidatorAreaCode(m.Province) && !areaCodeDictionary.ContainsKey(m.Province)) { areaCodeDictionary.Add(m.Province, string.Empty); } if (ValidatorAreaCode(m.City) && !areaCodeDictionary.ContainsKey(m.City)) { areaCodeDictionary.Add(m.City, string.Empty); } if (ValidatorAreaCode(m.Region) && !areaCodeDictionary.ContainsKey(m.Region)) { areaCodeDictionary.Add(m.Region, string.Empty); } if (ValidatorAreaCode(m.Township) && !areaCodeDictionary.ContainsKey(m.Township)) { areaCodeDictionary.Add(m.Township, string.Empty); } if (ValidatorAreaCode(m.Village) && !areaCodeDictionary.ContainsKey(m.Village)) { areaCodeDictionary.Add(m.Village, string.Empty); } return(m); }).Count(); var areaCodeList = areaCodeDictionary.Keys.ToList(); var areaInfoList = _areaRepository.GetAll(p => areaCodeList.Contains(p.AID)); HSSFWorkbook workbook = new HSSFWorkbook(); MemoryStream ms = new MemoryStream(); // 创建一张工作薄。 var workSheet = workbook.CreateSheet("注册会员列表"); var headerRow = workSheet.CreateRow(0); var tableHeaderTexts = new string[] { "用户编号", "姓名", "电话", "省", "市", "区/县", "乡镇", "村", "亩数", "用户类型", "注册时间" }; ICellStyle style = workbook.CreateCellStyle(); style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; style.FillPattern = FillPattern.SolidForeground; //生成列头 for (int i = 0; i < tableHeaderTexts.Length; i++) { var currentCell = headerRow.CreateCell(i); currentCell.SetCellValue(tableHeaderTexts[i]); currentCell.CellStyle = style; } var currentRoeIndex = 0; //给用地区信息赋上说明 foreach (var user in modelList) { currentRoeIndex++; var provinceInfo = areaInfoList.FirstOrDefault(p => p.AID == user.Province); var cityInfo = areaInfoList.FirstOrDefault(p => p.AID == user.City); var regionInfo = areaInfoList.FirstOrDefault(p => p.AID == user.Region); var townshipInfo = areaInfoList.FirstOrDefault(p => p.AID == user.Township); var villageInfo = areaInfoList.FirstOrDefault(p => p.AID == user.Village); if (provinceInfo != null) { user.Province = provinceInfo.DisplayName; } else { user.Province = string.Empty; } if (cityInfo != null) { user.City = cityInfo.DisplayName; } else { user.City = string.Empty; } if (regionInfo != null) { user.Region = regionInfo.DisplayName; } else { user.Region = string.Empty; } if (townshipInfo != null) { user.Township = townshipInfo.DisplayName; } else { user.Township = string.Empty; } if (villageInfo != null) { user.Village = villageInfo.DisplayName; } else { user.Village = string.Empty; } var dataRow = workSheet.CreateRow(currentRoeIndex); var userIdCell = dataRow.CreateCell(0); var userNameCell = dataRow.CreateCell(1); var phoneNumberCell = dataRow.CreateCell(2); var provinceCell = dataRow.CreateCell(3); var cityCell = dataRow.CreateCell(4); var regionCell = dataRow.CreateCell(5); var townshipCell = dataRow.CreateCell(6); var villageCell = dataRow.CreateCell(7); var landCell = dataRow.CreateCell(8); var userTypeCell = dataRow.CreateCell(9); var registryTimeCell = dataRow.CreateCell(10); userIdCell.SetCellValue(user.UserId); userNameCell.SetCellValue(user.UserName); phoneNumberCell.SetCellValue(user.PhoneNumber); provinceCell.SetCellValue(user.Province); cityCell.SetCellValue(user.City); regionCell.SetCellValue(user.Region); townshipCell.SetCellValue(user.Township); villageCell.SetCellValue(user.Village); landCell.SetCellValue(Convert.ToString(user.Land)); userTypeCell.SetCellValue(((UserTypes)user.Type).GetDescription()); registryTimeCell.SetCellValue(user.CreateTime.ToString("yyyy.MM.dd")); } workbook.Write(ms); Response.AddHeader("Content-Disposition", string.Format("attachment;filename=MemberUserList" + (DateTime.Now.ToString("yyyyMMddHHmmss")) + ".xls")); Response.BinaryWrite(ms.ToArray()); workbook = null; return(File(ms, "application/ms-excel")); } else { return(Content("no data")); } }