// // GET: /Service/Manage/ public override ActionResult Index() { var type = Request["type"]; var filter = !string.IsNullOrEmpty(type) ? t => t.ServiceType == (ServiceTypeEnum)Enum.Parse(typeof(ServiceTypeEnum), type) : (Expression <Func <ServiceList, bool> >)null; _typeFilter = filter; var hostList = new List <ListItem> { new ListItem("全部", "", true) }; hostList.AddRange(Rep.Distinct(t => t.Host, filter).Select(t => new ListItem(t, t))); ViewBag.ddlHost = hostList.ToArray(); ViewBag.TreeNodes = _tree.GetTreeNodes(filter); var list = Rep.QueryByPage(0, PageSize, out var count, filter); ViewBag.RecordCount = count; ViewBag.PageSize = PageSize; return(View(list)); }
public ActionResult ExportToExcel() { int count; var list = ((List <ServiceList>)Session["list"] ?? Rep.QueryByPage(0, int.MaxValue, out count)).Where( _typeFilter.Compile()).ToList(); const string thHtml = "<th>{0}</th>"; const string tdHtml = "<td>{0}</td>"; var sb = new StringBuilder(); sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">"); sb.Append("<tr>"); sb.AppendFormat(thHtml, ""); sb.AppendFormat(thHtml, "服务ID"); sb.AppendFormat(thHtml, "二级服务ID"); sb.AppendFormat(thHtml, "内网IP"); sb.AppendFormat(thHtml, "内网端口"); sb.AppendFormat(thHtml, "外网IP"); sb.AppendFormat(thHtml, "外网端口"); sb.AppendFormat(thHtml, "服务名称"); sb.AppendFormat(thHtml, "二级服务名称"); sb.AppendFormat(thHtml, "版本"); sb.AppendFormat(thHtml, "备注"); sb.Append("</tr>"); var rowIndex = 1; foreach (var item in list) { sb.Append("<tr>"); sb.AppendFormat(tdHtml, rowIndex++); sb.AppendFormat(tdHtml, item.PrimaryId); sb.AppendFormat(tdHtml, item.SecondaryId); var conf = new ServiceConf(); if (!string.IsNullOrWhiteSpace(item.RegContent)) { conf = JsonConvert.DeserializeObject <ServiceConf>(item.RegContent); } var inList = conf.InAddr ?? new List <string>(); var outList = conf.OutAddr ?? new List <string>(); var inFlag = inList.FirstOrDefault(t => t.Contains(item.Host)) != null; var inAddr = inFlag ? inList.First(t => t.Contains(item.Host)) .Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries) : new string[0]; sb.AppendFormat(tdHtml, item.Host); sb.AppendFormat(tdHtml, inFlag ? inAddr.Length > 1 ? inAddr[1] : "" : ""); var outAddr = new List <string>(); var outPort = new List <string>(); if (outList.Any()) { var @out = outList.Select(t => { var arr = t.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); return(new { Addr = arr[0], Port = arr.Length > 1 ? arr[1] : "" }); }).ToList(); outAddr = @out.Select(t => t.Addr).ToList(); outPort = @out.Select(t => t.Port).ToList(); } sb.AppendFormat(tdHtml, string.Join(",", outAddr)); sb.AppendFormat(tdHtml, string.Join(",", outPort)); sb.AppendFormat(tdHtml, item.ServiceName); sb.AppendFormat(tdHtml, item.SecondaryName); sb.AppendFormat(tdHtml, item.Version); sb.AppendFormat(tdHtml, item.Remark); sb.Append("</tr>"); } sb.Append("</table>"); return(File(Encoding.UTF8.GetBytes(sb.ToString()), "application/excel", "服务列表.xls")); }