/// <summary> /// delete url (delete) /// </summary> public static string DeleteUrl(string url, IHttpClientFactory client, string key = null, int version = 1, int minor = 1, string mediaType = "application/json") { try { HttpClient http; if (string.IsNullOrEmpty(key)) { http = client.CreateClient(); } else { http = client.CreateClient(key); } var handle = new HttpRequestMessage(); handle.Version = new Version(version, minor); handle.Content = new StringContent("", Encoding.UTF8, mediaType); handle.Method = HttpMethod.Delete; handle.RequestUri = new Uri(url); var response = http.SendAsync(handle).Result; handle.Content?.Dispose(); handle.Dispose(); return(response.Content.ReadAsStringAsync().Result); } catch (Exception ex) { Task.Factory.StartNew(() => { BaseLog.SaveLog(url + ":" + ex.ToString(), "PutUrl_exp"); }); BaseLog.SaveLog(url + ":" + ex.ToString(), "DeleteUrl_exp"); return(null); } }
/// <summary> /// post content(insert) /// </summary> public static string PostSoap(string url, string method, Dictionary <string, object> param, IHttpClientFactory client, string key = null, string Namespace = "http://tempuri.org/", int version = 1, int minor = 1) { try { HttpClient http; if (string.IsNullOrEmpty(key)) { http = client.CreateClient(); } else { http = client.CreateClient(key); } var xml = new StringBuilder(); xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); xml.Append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"); xml.Append("<soap:Header />"); xml.Append("<soap:Body>"); xml.AppendFormat("<{0} xmlns=\"{1}\">", method, Namespace); foreach (KeyValuePair <string, object> item in param) { xml.AppendFormat("<{0}>{1}</{0}>", item.Key, item.Value.ToStr().Replace("<", "<").Replace(">", ">")); } xml.AppendFormat("</{0}>", method); xml.Append("</soap:Body>"); xml.Append("</soap:Envelope>");; var handle = new HttpRequestMessage(); handle.Version = new Version(version, minor); handle.Content = new StringContent(xml.ToString(), Encoding.UTF8, "text/xml"); handle.Method = HttpMethod.Post; handle.RequestUri = new Uri(url); var response = http.SendAsync(handle).Result; handle.Content.Dispose(); handle.Dispose(); var result = response.Content.ReadAsStringAsync().Result; result = result.Replace("soap:Envelope", "Envelope"); result = result.Replace("soap:Body", "Body"); result = result.Replace(" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"", ""); result = result.Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", ""); result = result.Replace(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", ""); result = result.Replace(" xmlns=\"http://openmas.chinamobile.com/pulgin\"", ""); result = result.Replace(string.Format(" xmlns=\"{0}\"", Namespace), ""); return(BaseXml.GetXmlString(result, string.Format("Envelope/Body/{0}Response/{0}Result", method)).Replace("<", "<").Replace(">", ">")); } catch (Exception ex) { Task.Factory.StartNew(() => { BaseLog.SaveLog(url + ":" + ex.ToString(), "PostSoap_exp"); }); return(null); } }
/// <summary> /// post url(insert) /// </summary> public static string PostUrl(string url, Dictionary <string, object> dic, IHttpClientFactory client, string key = null, int version = 1, int minor = 1, string mediaType = "application/json") { try { HttpClient http; if (string.IsNullOrEmpty(key)) { http = client.CreateClient(); } else { http = client.CreateClient(key); } var count = 0; foreach (var item in dic) { if (url.Contains("?")) { url = string.Format("{0}&{1}={2}", url, item.Key, item.Value); } else { if (count == 0) { url = string.Format("{0}?{1}={2}", url, item.Key, item.Value); } else { url = string.Format("{0}&{1}={2}", url, item.Key, item.Value); } } count++; } var handle = new HttpRequestMessage(); handle.Version = new Version(version, minor); handle.Content = new StringContent("", Encoding.UTF8, mediaType); handle.Method = HttpMethod.Post; handle.RequestUri = new Uri(url); var response = http.SendAsync(handle).Result; handle.Content?.Dispose(); handle.Dispose(); return(response.Content.ReadAsStringAsync().Result); } catch (Exception ex) { Task.Factory.StartNew(() => { BaseLog.SaveLog(url + ":" + ex.ToString(), "PostUrl_exp"); }); return(null); } }
/// <summary> /// 获取excel流 /// </summary> /// <returns></returns> public static byte[] Result(ExcelModel model, Dictionary <string, object> title) { try { //自动列宽 var i = 0; title.ToList().ForEach(a => { model.sheet.AutoSizeColumn(i++, true); model.sheet.Autobreaks = true; model.sheet.HorizontallyCenter = true; }); for (int rowNum = 2; rowNum <= model.sheet.LastRowNum; rowNum++) { var currentRow = model.sheet.GetRow(rowNum); var height = 0; for (var col = 0; col < title.Count; col++) { var currentCell = currentRow.GetCell(col); if (currentCell != null) { var length = System.Text.Encoding.UTF8.GetBytes(currentCell.ToString()).Length; if ((20 * (length / 60 + 1)) > height) { height = 20 * (length / 60 + 1); } } } currentRow.HeightInPoints = height; } using (var file = new MemoryStream()) { model.workbook.Write(file); return(file.ToArray()); } } catch (Exception ex) { BaseLog.SaveLog(ex.ToString(), "ToExcel.Result"); return(null); } }
/// <summary> /// 初始化excel /// </summary> /// <param name="headerText">标题</param> /// <param name="title">表头</param> /// <returns></returns> public static ExcelModel Init(string headerText, List <Dictionary <string, object> > title1, Dictionary <string, object> title2) { try { var result = new ExcelModel(); result.workbook = new HSSFWorkbook(); InitializeWorkbook(result.workbook); result.sheet = result.workbook.CreateSheet(headerText); result.row = result.sheet.CreateRow(0); result.cell = result.row.CreateCell(0); result.cell.SetCellValue(headerText); var style = result.workbook.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center; var font = result.workbook.CreateFont(); font.FontHeight = 20 * 20; style.SetFont(font); result.cell.CellStyle = style; result.sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, title2.Count - 1)); var i = 0; if (title1 != null) { var step = 0; result.row = result.sheet.CreateRow(1); title1.ToList().ForEach(a => { if (i == 0) { result.cell = result.row.CreateCell(i++); } else { result.cell = result.row.CreateCell(step); } result.cell.Row.Height = 420; result.cell.SetCellValue(a.GetValue("text").ToStr()); result.sheet.AddMergedRegion(new CellRangeAddress(1, 1, step, step + a.GetValue("step").ToStr().ToInt(0) - 1)); result.cell.CellStyle = GetStyle(result.workbook, true); step = step + a.GetValue("step").ToStr().ToInt(0); }); } result.row = result.sheet.CreateRow(2); i = 0; title2.ToList().ForEach(a => { result.cell = result.row.CreateCell(i++); result.cell.Row.Height = 420; result.cell.SetCellValue(a.Value.ToStr()); result.cell.CellStyle = GetStyle(result.workbook, true); }); return(result); } catch (Exception ex) { BaseLog.SaveLog(ex.ToString(), "ToExcel.Init"); return(null); } }
/// <summary> /// 填充内容 /// </summary> /// <param name="listContent">内容列表</param> /// <param name="model"></param> public static void FillContent(List <Dictionary <string, object> > listContent, ExcelModel model, string exclude = "", bool IsSmallTile = false) { try { //插入查询结果 var i = 0; if (listContent != null) { model.style_n = GetStyle(model.workbook, true, true); model.style = GetStyle(model.workbook); foreach (var item in listContent) { if (IsSmallTile) { model.row = model.sheet.CreateRow(i + 3); } else { model.row = model.sheet.CreateRow(i + 2); } int j = 0; foreach (var temp in item) { if (temp.Key.ToLower() == exclude.ToLower()) { continue; } model.cell = model.row.CreateCell(j++); model.cell.Row.Height = 420; if (temp.Value is Dictionary <string, object> ) { var info = temp.Value as Dictionary <string, object>; model.sheet.AddMergedRegion(new CellRangeAddress( info.GetValue("rowbegin").ToStr().ToInt(0), info.GetValue("rowend").ToStr().ToInt(0), info.GetValue("colbegin").ToStr().ToInt(0) - 1, info.GetValue("colend").ToStr().ToInt(0) - 1)); model.cell.SetCellValue(info.GetValue("text").ToStr()); } else { model.cell.SetCellValue(temp.Value.ToStr()); } if (temp.Value.ToStr().Contains("\n")) { model.cell.CellStyle = model.style_n; } else { model.cell.CellStyle = model.style; } } i++; } } } catch (Exception ex) { BaseLog.SaveLog(ex.ToString(), "ToExcel.FillContent"); } }