一、Server.MapPath的用法
1).Server.MapPath("./") 当前目录
2).Server.MapPath("/") 网站主目录
3).Server.MapPath("../") 上层目录
4).Server.MapPath("~/") 网站虚拟目录
5).控制台程序获取:string path = Environment.CurrentDirectory.ToLower().Replace("bin\debug","")
二、获取文件后缀
System.IO.Path.GetExtension
三、NPOI组件的用法(将excel转化为DataTable)
四、ToDictionary、ToLookup和Linq
五、js替换指定的url参数,没有则添加,有则修改
function replaceParamVal(paramName, replaceWith) {
var oUrl = this.location.href.toString();
var nUrl = oUrl;
var re1 = eval('/(&' + paramName + '=)([^&]*)/gi');
var re2 = eval('/(\?' + paramName + '=)([^&]*)/gi');
if (re1.test(oUrl)) {
nUrl = oUrl.replace(re1, "&" + paramName + '=' + replaceWith);
} else if (re2.test(oUrl)) {
nUrl = oUrl.replace(re2, "?" + paramName + '=' + replaceWith);
}
else {
if (oUrl.indexOf("?") != -1) {
nUrl += "&"+paramName+"="+replaceWith;
} else {
nUrl += "?" + paramName + "=" + replaceWith;
}
}
this.location = nUrl;
}
六、js 滚动条滚动到底部
$(window).scroll(function(){
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
if(scrollTop + windowHeight == scrollHeight){
alert("you are in the bottom");
}
});
/// <summary>
/// 输出Excel
/// </summary>
/// <param name="workBook"></param>
/// <param name="fileName"></param>
/// <param name="contentEncode"></param>
public static void Export(IWorkbook workBook, string fileName)
{
MemoryStream ms = new MemoryStream();
workBook.Write(ms);
HttpResponse httpResponse = HttpContext.Current.Response;
httpResponse.AddHeader("Pragma", "public");
httpResponse.AddHeader("Cache-Control", "max-age=0");
httpResponse.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
//httpResponse.AddHeader("content-type", "application/x-msdownload");
httpResponse.ContentEncoding = Encoding.UTF8;
httpResponse.ContentType = "application/vnd.ms-excel";
httpResponse.BinaryWrite(ms.ToArray());
workBook = null;
ms.Close();
ms.Dispose();
}
九、判断值是否为指定类型
System.ComponentModel.TypeDescriptor.GetConverter(item.Type).ConvertFrom(dr[item.Key].ToString());
十、动态拼接json
//递归构建json
static string RecursionMosaicJson(List<ExportsOrderInterfaceFieldVM> fieldList)
{
StringBuilder sb = new StringBuilder("{");
foreach (var field in fieldList)
{
if (parentKeyList.Contains(field.ParentID))
{
continue;
}
if (field.ObjectType == InterfaceObjectType.Array.ToString())
{
List<ExportsOrderInterfaceFieldVM> list = fieldAllList.Where(l => l.ParentID == field.Id).ToList();
string childrenJson = RecursionMosaicJson(list);
parentKeyList.Add(field.Id);
sb.AppendFormat(""{0}":[{1}],", field.InterfaceFieldName, childrenJson);
}
else if (field.ObjectType == InterfaceObjectType.Object.ToString())
{
sb.AppendFormat(""{0}":"",", field.InterfaceFieldName);
}
}
string json = sb.ToString().TrimEnd(',');
json += "}";
return json;
}
十一、动态拼接xml
//递归构建xml static string RecursionMosaicXml(List<ExportsOrderInterfaceFieldVM> fieldList) { StringBuilder sb = new StringBuilder(""); foreach (var field in fieldList) { if (parentKeyList.Contains(field.ParentID)) { continue; } //xml中的属性 List<ExportsOrderInterfaceFieldVM> attributeList = fieldAllList.Where(l => l.XMLElementType == XmlElementType.Attribute.ToString() && l.ParentID == field.Id).ToList(); string attrXml = ""; if (attributeList.Count > 0) { foreach (var attr in attributeList) { attrXml += " " + attr.InterfaceFieldName + "="""; } } if (field.ObjectType == InterfaceObjectType.Array.ToString()) { List<ExportsOrderInterfaceFieldVM> list = fieldAllList.Where(l => l.ParentID == field.Id && l.XMLElementType!=XmlElementType.Attribute.ToString()).ToList(); string childrenXml = RecursionMosaicXml(list); parentKeyList.Add(field.Id); sb.AppendFormat("<{0}{2}>{1}</{0}>", field.InterfaceFieldName, childrenXml,attrXml); } else if(field.ObjectType == InterfaceObjectType.Object.ToString()) { sb.AppendFormat("<{0}{1}></{0}>", field.InterfaceFieldName, attrXml); } } string xml = sb.ToString(); return xml; }
十二、不常用时间转换
//源格式 字符串转为时间 20170709023945000-0700 string refreshTokenTimeout = model.refresh_token_timeout; DateTime dt = DateTime.ParseExact(refreshTokenTimeout, "yyyyMMddHHmmssfffzz00", new CultureInfo("en-GB"));
//源格式 时间转化为字符串
string time = DateTime.Now.ToString("yyyyMMddHHmmssfffzz00");