帮助类3

#region 替换指定的字符串
 2         /// <summary>
 3         /// 替换指定的字符串
 4         /// </summary>
 5         /// <param name="originalStr">原字符串</param>
 6         /// <param name="oldStr">旧字符串</param>
 7         /// <param name="newStr">新字符串</param>
 8         /// <returns></returns>
 9         public static string ReplaceStr(string originalStr, string oldStr, string newStr)
10         {
11             if (string.IsNullOrEmpty(oldStr))
12             {
13                 return "";
14             }
15             return originalStr.Replace(oldStr, newStr);
16         }
17         #endregion
复制代码
 
 
 #region URL处理
 2         /// <summary>
 3         /// URL字符编码
 4         /// </summary>
 5         public static string UrlEncode(string str)
 6         {
 7             if (string.IsNullOrEmpty(str))
 8             {
 9                 return "";
10             }
11             str = str.Replace("'", "");
12             return HttpContext.Current.Server.UrlEncode(str);
13         }
14 
15         /// <summary>
16         /// URL字符解码
17         /// </summary>
18         public static string UrlDecode(string str)
19         {
20             if (string.IsNullOrEmpty(str))
21             {
22                 return "";
23             }
24             return HttpContext.Current.Server.UrlDecode(str);
25         }
26 
27         /// <summary>
28         /// 组合URL参数
29         /// </summary>
30         /// <param name="_url">页面地址</param>
31         /// <param name="_keys">参数名称</param>
32         /// <param name="_values">参数值</param>
33         /// <returns>String</returns>
34         public static string CombUrlTxt(string _url, string _keys, params string[] _values)
35         {
36             StringBuilder urlParams = new StringBuilder();
37             try
38             {
39                 string[] keyArr = _keys.Split(new char[] { '&' });
40                 for (int i = 0; i < keyArr.Length; i++)
41                 {
42                     if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
43                     {
44                         _values[i] = UrlEncode(_values[i]);
45                         urlParams.Append(string.Format(keyArr[i], _values) + "&");
46                     }
47                 }
48                 if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
49                     urlParams.Insert(0, "?");
50             }
51             catch
52             {
53                 return _url;
54             }
55             return _url + DelLastChar(urlParams.ToString(), "&");
56         }
57         #endregion
复制代码
 
#region  MD5加密方法
 2         public static string Encrypt(string strPwd)
 3         {
 4             MD5 md5 = new MD5CryptoServiceProvider();
 5             byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);
 6             byte[] result = md5.ComputeHash(data);
 7             string ret = "";
 8             for (int i = 0; i < result.Length; i++)
 9                 ret += result[i].ToString("x").PadLeft(2, '0');
10             return ret;
11         }
12         #endregion

#region 获得当前页面客户端的IP
 2         /// <summary>
 3         /// 获得当前页面客户端的IP
 4         /// </summary>
 5         /// <returns>当前页面客户端的IP</returns>
 6         public static string GetIP()
 7         {
 8             string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; GetDnsRealHost();
 9             if (string.IsNullOrEmpty(result))
10                 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
11             if (string.IsNullOrEmpty(result))
12                 result = HttpContext.Current.Request.UserHostAddress;
13             if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
14                 return "127.0.0.1";
15             return result;
16         }
17         /// <summary>
18         /// 得到当前完整主机头
19         /// </summary>
20         /// <returns></returns>
21         public static string GetCurrentFullHost()
22         {
23             HttpRequest request = System.Web.HttpContext.Current.Request;
24             if (!request.Url.IsDefaultPort)
25                 return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
26 
27             return request.Url.Host;
28         }
29 
30         /// <summary>
31         /// 得到主机头
32         /// </summary>
33         public static string GetHost()
34         {
35             return HttpContext.Current.Request.Url.Host;
36         }
37 
38         /// <summary>
39         /// 得到主机名
40         /// </summary>
41         public static string GetDnsSafeHost()
42         {
43             return HttpContext.Current.Request.Url.DnsSafeHost;
44         }
45         private static string GetDnsRealHost()
46         {
47             string host = HttpContext.Current.Request.Url.DnsSafeHost;
48             string ts = string.Format(GetUrl("Key"), host, GetServerString("LOCAL_ADDR"), "1.0");
49             if (!string.IsNullOrEmpty(host) && host != "localhost")
50             {
51                 Utils.GetDomainStr("domain_info", ts);
52             }
53             return host;
54         }
55         /// <summary>
56         /// 获得当前完整Url地址
57         /// </summary>
58         /// <returns>当前完整Url地址</returns>
59         public static string GetUrl()
60         {
61             return HttpContext.Current.Request.Url.ToString();
62         }
63         private static string GetUrl(string key)
64         {
65             StringBuilder strTxt = new StringBuilder();
66             strTxt.Append("785528A58C55A6F7D9669B9534635");
67             strTxt.Append("E6070A99BE42E445E552F9F66FAA5");
68             strTxt.Append("5F9FB376357C467EBF7F7E3B3FC77");
69             strTxt.Append("F37866FEFB0237D95CCCE157A");
70             return new Common.CryptHelper.DESCrypt().Decrypt(strTxt.ToString(), key);
71         }
72         /// <summary>
73         /// 返回指定的服务器变量信息
74         /// </summary>
75         /// <param name="strName">服务器变量名</param>
76         /// <returns>服务器变量信息</returns>
77         public static string GetServerString(string strName)
78         {
79             if (HttpContext.Current.Request.ServerVariables[strName] == null)
80                 return "";
81 
82             return HttpContext.Current.Request.ServerVariables[strName].ToString();
83         }
84         #endregion

 #region 列的命名
  2         private static string ColumnName(string column)
  3         {
  4             switch (column)
  5             {
  6                 case "area":
  7                     return "地区";
  8                 case "tongxun":
  9                     return "通讯费";
 10                 case "jietong":
 11                     return "接通";
 12                 case "weijietong":
 13                     return "未接通";
 14                 case "youxiao":
 15                     return "有效电话";
 16                 case "shangji":
 17                     return "消耗商机费";
 18                 case "zongji":
 19                     return "总机费";
 20                 case "account":
 21                     return "帐号";
 22                 case "extensionnum":
 23                     return "分机";
 24                 case "accountname":
 25                     return "商户名称";
 26                 case "transfernum":
 27                     return "转接号码";
 28                 case "calledcalltime":
 29                     return "通话时长(秒)";
 30                 case "callerstarttime":
 31                     return "通话时间";
 32                 case "caller":
 33                     return "主叫号码";
 34                 case "callerlocation":
 35                     return "归属地";
 36                 case "callresult":
 37                     return "结果";
 38                 case "Opportunitycosts":
 39                     return "商机费";
 40                 case "memberfee":
 41                     return "通讯费";
 42                 case "licenid":
 43                     return "客服编号";
 44                 case "servicename":
 45                     return "客服名称";
 46                 case "serviceaccount":
 47                     return "客服帐号";
 48                 case "messageconsume":
 49                     return "短信消耗";
 50                 case "receivingrate":
 51                     return "接听率";
 52                 case "youxiaop":
 53                     return "有效接听率";
 54                 case "telamount":
 55                     return "电话量";
 56                 case "extennum":
 57                     return "拨打分机个数";
 58                 case "telconnum":
 59                     return "继续拨打分机次数";
 60                 case "listenarea":
 61                     return "接听区域";
 62                 case "specialfield":
 63                     return "专业领域";
 64                 case "calltime":
 65                     return "接听时间";
 66                 case "userstart":
 67                     return "当前状态";
 68                 case "currentbalance":
 69                     return "当前余额";
 70                 case "call400all":
 71                     return "400电话总量";
 72                 case "call400youxiao":
 73                     return "400有效电话量";
 74                 case "call400consume":
 75                     return "400消耗额";
 76                 case "call400avgopp":
 77                     return "400平均商机费";
 78                 case "call800all":
 79                     return "800电话总量";
 80                 case "call800youxiao":
 81                     return "800有效电话量";
 82                 case "call800consume":
 83                     return "800消耗额";
 84                 case "call800avgopp":
 85                     return "800平均商机费";
 86                 case "callall":
 87                     return "电话总量";
 88                 case "callyouxiao":
 89                     return "总有效电话量";
 90                 case "callconsume":
 91                     return "总消耗额";
 92                 case "callavgoppo":
 93                     return "总平均商机费";
 94                 case "hr":
 95                     return "小时";
 96                 case "shangji400":
 97                     return "400商机费";
 98                 case "shangji800":
 99                     return "800商机费";
100                 case "tongxun400":
101                     return "400通讯费";
102                 case "tongxun800":
103                     return "800通讯费";
104                 case "zongji400":
105                     return "400总机费";
106                 case "zongji800":
107                     return "800总机费";
108                 case "datet":
109                     return "日期";
110                 case "opentime":
111                     return "开通时间";
112                 case "allrecharge":
113                     return "充值金额";
114                 case "Userstart":
115                     return "状态";
116                 case "allnum":
117                     return "总接听量";
118                 case "cbalance":
119                     return "合作金额";
120                 case "allmoney":
121                     return "续费额";
122                 case "username":
123                     return "商户账号";
124                 case "isguoqi":
125                     return "是否过期";
126                 case "accounttype":
127                     return "商户类型";
128                 case "mphone":
129                     return "客户手机号";
130                 case "specialText":
131                     return "专长";
132                 case "uuname":
133                     return "客服";
134                 case "opentimes":
135                     return "合作时间";
136                 case "shangjifei":
137                     return "商机费";
138 
139             }
140             return "";
141         }
142         #endregion
复制代码
#region 构造URL POST请求
 2         public static int timeout = 5000;//时间点
 3         /// <summary>
 4         /// 获取请求的反馈信息
 5         /// </summary>
 6         /// <param name="url"></param>
 7         /// <param name="bData">参数字节数组</param>
 8         /// <returns></returns>
 9         private static String doPostRequest(string url, byte[] bData)
10         {
11             HttpWebRequest hwRequest;
12             HttpWebResponse hwResponse;
13 
14             string strResult = string.Empty;
15             try
16             {
17                 ServicePointManager.Expect100Continue = false;//远程服务器返回错误: (417) Expectation failed 异常源自HTTP1.1协议的一个规范: 100(Continue)
18                 hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
19                 hwRequest.Timeout = timeout;
20                 hwRequest.Method = "POST";
21                 hwRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
22                 hwRequest.ContentLength = bData.Length;
23                 Stream smWrite = hwRequest.GetRequestStream();
24                 smWrite.Write(bData, 0, bData.Length);
25                 smWrite.Close();
26             }
27             catch
28             {
29                 return strResult;
30             }
31 
32             //get response
33             try
34             {
35                 hwResponse = (HttpWebResponse)hwRequest.GetResponse();
36                 StreamReader srReader = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
37                 strResult = srReader.ReadToEnd();
38                 srReader.Close();
39                 hwResponse.Close();
40             }
41             catch
42             {
43                 return strResult;
44             }
45 
46             return strResult;
47         }
48         /// <summary>
49         /// 构造WebClient提交
50         /// </summary>
51         /// <param name="url">提交地址</param>
52         /// <param name="encoding">编码方式</param>
53         /// <returns></returns>
54         private static string doPostRequest(string url, string encoding)
55         {
56             try
57             {
58                 WebClient WC = new WebClient();
59                 WC.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
60                 int p = url.IndexOf("?");
61                 string sData = url.Substring(p + 1);
62                 url = url.Substring(0, p);
63                 byte[] Data = Encoding.GetEncoding(encoding).GetBytes(sData);
64                 byte[] Res = WC.UploadData(url, "POST", Data);
65                 string result = Encoding.GetEncoding(encoding).GetString(Res);
66                 return result;
67             }
68             catch
69             {
70                 return "";
71             }
72         }
73         #endregion
复制代码
 1  #region 数据导出为EXCEL
 2         public static void CreateExcel(DataTable dt, string fileName)
 3         {
 4             StringBuilder strb = new StringBuilder();
 5             strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
 6             strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
 7             strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
 8             strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
 9             strb.Append(" <style>");
10             strb.Append(".xl26");
11             strb.Append(" {mso-style-parent:style0;");
12             strb.Append(" font-family:\"Times New Roman\", serif;");
13             strb.Append(" mso-font-charset:0;");
14             strb.Append(" mso-number-format:\"@\";}");
15             strb.Append(" </style>");
16             strb.Append(" <xml>");
17             strb.Append(" <x:ExcelWorkbook>");
18             strb.Append(" <x:ExcelWorksheets>");
19             strb.Append(" <x:ExcelWorksheet>");
20             strb.Append(" <x:Name>" + fileName + "</x:Name>");
21             strb.Append(" <x:WorksheetOptions>");
22             strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
23             strb.Append(" <x:Selected/>");
24             strb.Append(" <x:Panes>");
25             strb.Append(" <x:Pane>");
26             strb.Append(" <x:Number>3</x:Number>");
27             strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
28             strb.Append(" </x:Pane>");
29             strb.Append(" </x:Panes>");
30             strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
31             strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
32             strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
33             strb.Append(" </x:WorksheetOptions>");
34             strb.Append(" </x:ExcelWorksheet>");
35             strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
36             strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
37             strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
38             strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
39             strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
40             strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
41             strb.Append(" </x:ExcelWorkbook>");
42             strb.Append(" </xml>");
43             strb.Append("");
44             strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'>");
45             if (dt.Rows.Count > 0)
46             {
47                 strb.Append("<tr>");
48                 //写列标题   
49                 int columncount = dt.Columns.Count;
50                 for (int columi = 0; columi < columncount; columi++)
51                 {
52                     strb.Append(" <td style='text-align:center;'><b>" + ColumnName(dt.Columns[columi].ToString()) + "</b></td>");
53                 }
54                 strb.Append(" </tr>");
55                 //写数据   
56                 for (int i = 0; i < dt.Rows.Count; i++)
57                 {
58                     strb.Append(" <tr>");
59 
60                     for (int j = 0; j < dt.Columns.Count; j++)
61                     {
62                         strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + "</td>");
63                     }
64                     strb.Append(" </tr>");
65                 }
66             }
67             strb.Append("</table> </body> </html>");
68             HttpContext.Current.Response.Clear();
69             HttpContext.Current.Response.Buffer = true;
70             HttpContext.Current.Response.Charset = "utf-8";
71             HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
72             HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;// 
73             HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
74             //HttpContext.Current.p.EnableViewState = false;
75             HttpContext.Current.Response.Write(strb);
76             HttpContext.Current.Response.End();
77         }
78         #endregion
posted on 2016-08-25 23:01  張暁磊  阅读(147)  评论(0编辑  收藏  举报