/// <summary> /// 公式计算 /// </summary> /// <typeparam name="T">普通类型数据,例:int</typeparam> /// <param name="formula">公式,例:function Calc(x, y) { return x + y; }</param> /// <param name="parameterList">参数列表,Dictionary<string, object> 或 new {}</param> /// <returns></returns> public static T Calc <T>(string formula, object parameterList = null) { if (string.IsNullOrWhiteSpace(formula)) { throw new Exception(FormulaNullException); } #region 参数数据映射 Dictionary <string, object> paramDict = CommonHelper.GetParameterDict(parameterList); #endregion #region 公式参数映射 // 如果存在参数信息 if (paramDict != null) { foreach (KeyValuePair <string, object> keyValueItem in paramDict) { formula = formula.Replace(keyValueItem.Key, keyValueItem.Value.ToString()); } } #endregion #region 计算公式结果 // 使用 COM 组件计算公式 MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControlClass() { Language = "JavaScript" }; object value = scriptControl.Eval(formula); #endregion return((T)Convert.ChangeType(value, typeof(T))); }
/// <summary> /// 设置 Cookie 数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="cookieName">Cookie 名称</param> /// <param name="data">实体数据</param> /// <param name="expires">过期时间</param> /// <param name="httpResponse">HttpResponseBase,如果未指定则取自 HttpContext.Current.Response</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="propertyList">属性列表,如果指定,则按指定属性列表设置 Cookie 数据</param> /// <param name="propertyContain">是否包含,true 属性包含,flase 属性排除</param> /// <param name="reflectionType">反射类型</param> public static void SetCookieT <T>(string cookieName, T data, DateTime expires, HttpResponseBase httpResponse = null, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { List <string> filterNameList = null; if (propertyList != null) { filterNameList = propertyList.ToList <string>(); } Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); ExecuteSetCookie <T>(httpResponse, cookieName, data, expires, propertyDict, filterNameList, propertyContain, reflectionType); }
private static void SetWorkbookData <T>(IWorkbook workbook, List <T> dataList, string sheetName, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, Action <ICell, object> cellCallback = null, Action <ISheet, List <string> > sheetCallback = null, bool isHeader = true, object columnValueFormat = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <string> filterNameList = null; if (propertyList != null) { filterNameList = propertyList.ToList <string>(); } Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); Dictionary <string, object> valueFormatDict = CommonHelper.GetParameterDict(columnValueFormat); ToSheet(workbook, dataList, sheetName, cellCallback, sheetCallback, isHeader, filterNameList, propertyContain, propertyDict, valueFormatDict, reflectionType); }
/// <summary> /// 根据实体数据列表创建 Excel /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataList">实体数据列表</param> /// <param name="excelPath">Excel 路径</param> /// <param name="sheetName">Sheet 表单名称</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="propertyList">属性列表,如果指定,则按指定属性列表生成 Excel</param> /// <param name="propertyContain">是否包含,true 属性包含,flase 属性排除</param> /// <param name="columnValueFormat">列格式化,例:yyyy年MM月dd日</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static void ToExcel <T>(List <T> dataList, string excelPath, string sheetName, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, object columnValueFormat = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { ExecuteIWorkbookWrite(excelPath, (Workbook workbook) => { List <string> filterNameList = null; if (propertyList != null) { filterNameList = propertyList.ToList <string>(); } Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); Dictionary <string, object> valueFormatDict = CommonHelper.GetParameterDict(columnValueFormat); ToSheet(workbook, dataList, sheetName, filterNameList, propertyContain, propertyDict, valueFormatDict, reflectionType); }); }
private static Dictionary <int, PropertyInfo> InitEntityToTxtMapper <T>(object propertyMatchList = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal, string[] propertyList = null, bool propertyContain = true) where T : class { List <string> filterNameList = null; if (propertyList != null) { filterNameList = propertyList.ToList <string>(); } int propertyIndex = 0; Dictionary <string, object> propertyMatchDict = CommonHelper.GetParameterDict(propertyMatchList); Dictionary <int, PropertyInfo> propertyNameDict = new Dictionary <int, PropertyInfo>(); ReflectionGenericHelper.Foreach <T>((PropertyInfo propertyInfo) => { if (filterNameList == null || (propertyContain && filterNameList.IndexOf(propertyInfo.Name) >= 0) || (!propertyContain && filterNameList.IndexOf(propertyInfo.Name) < 0)) { if (propertyMatchDict != null && propertyMatchDict.ContainsKey(propertyInfo.Name)) { propertyNameDict.Add(int.Parse(propertyMatchDict[propertyInfo.Name].ToString()), propertyInfo); } else { if (typeEnum == TxtTypeEnum.Attribute) { TxtTAttribute attribute = propertyInfo.GetCustomAttribute <TxtTAttribute>(); if (attribute != null && (attribute.Type == AttributeReadAndWriteTypeEnum.ReadAndWrite || attribute.Type == AttributeReadAndWriteTypeEnum.Write)) { object attributeIndex = ReflectionExtendHelper.GetAttributeValue <TxtTAttribute>(typeof(T), propertyInfo, p => p.Index); if (attributeIndex != null) { propertyNameDict.Add(int.Parse(attributeIndex.ToString()), propertyInfo); } } } else { propertyNameDict.Add(propertyIndex, propertyInfo); propertyIndex++; } } } }); return(propertyNameDict); }
/// <summary> /// 格式化字符串 /// </summary> /// <param name="str">字符串,{} 表示格式化占位符</param> /// <param name="parameterList">占位符数据</param> /// <returns></returns> public static string Format(string str, object parameterList) { if (string.IsNullOrEmpty(str)) { return(str); } Dictionary <string, object> parameterDict = CommonHelper.GetParameterDict(parameterList); if (parameterDict == null || parameterDict.Count == 0) { return(str); } foreach (KeyValuePair <string, object> keyValueItem in parameterDict) { str = str.Replace("{" + keyValueItem.Key + "}", keyValueItem.Value.ToString()); } return(str); }
/// <summary> /// 格式化请求参数 /// </summary> /// <param name="parameters">参数</param> /// <param name="filePathDict"></param> /// <returns></returns> public static string FormatParameters(object parameters, Dictionary <string, string> filePathDict = null) { StringBuilder stringBuilder = new StringBuilder(); Dictionary <string, object> parameterDict = CommonHelper.GetParameterDict(parameters); if (parameterDict != null && parameterDict.Count > 0) { foreach (KeyValuePair <string, object> keyValueItem in parameterDict) { if (keyValueItem.Value != null && keyValueItem.Value.ToString().StartsWith(FILE_PREFIX)) { if (filePathDict != null) { filePathDict.Add(keyValueItem.Key, StringHelper.TrimStart(keyValueItem.Value.ToString(), FILE_PREFIX)); } continue; } stringBuilder.Append(string.Format("{0}={1}&", keyValueItem.Key, keyValueItem.Value)); } } return(stringBuilder.ToString().TrimEnd(new char[] { '&' })); }
/// <summary> /// 返回实体数据列表 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="txtPath">Txt 路径</param> /// <param name="splitChar">列分隔符,例:|</param> /// <param name="encoding">编码格式</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="typeEnum">TxtTypeEnum 枚举类型</param> /// <param name="lineSplitChar">行分隔符,例:\r\n</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static List <T> ToEntityList <T>(string txtPath, string splitChar, System.Text.Encoding encoding, object propertyMatchList = null, TxtTypeEnum typeEnum = TxtTypeEnum.Normal, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { List <T> dataList = new List <T>(); ExecuteStreamReader(txtPath, encoding, (StreamReader streamReader) => { Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); Dictionary <PropertyInfo, int> propertyNameDict = InitTxtToEntityMapper <T>(propertyDict, typeEnum); dynamic propertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); } string content = streamReader.ReadToEnd(); string[] contentList = content.Split(new string[] { lineSplitChar }, StringSplitOptions.None); foreach (string contentItem in contentList) { dataList.Add(TextToEntity <T>(contentItem, splitChar, propertySetDict, propertyNameDict)); } }); return(dataList); }
/// <summary> /// 请求数据 /// </summary> /// <param name="path">URL 地址</param> /// <param name="method">请求方法</param> /// <param name="contentType">ContentType 类型</param> /// <param name="parameters">参数数据,如果是上传文件,值需要加上前缀【file:】</param> /// <param name="headers">头部数据</param> /// <param name="customFun">自定义 HttpItem</param> /// <param name="encoding">编码格式</param> /// <returns></returns> public static HttpResult Request(string path, string method, string contentType, object parameters, object headers = null, Func <HttpItem, HttpItem> customFun = null, string encoding = "utf-8") { if (string.IsNullOrEmpty(method)) { method = MethodTypeEnum.GET; } if (string.IsNullOrEmpty(contentType)) { contentType = ContentTypeEnum.URL_ENCODED; } HttpHelper httpHelper = new HttpHelper(); HttpItem httpItem = new HttpItem() { URL = path, Method = method, ContentType = contentType }; #region 设置参数信息 if (parameters != null) { if (method == MethodTypeEnum.GET) { string parameterUrl = "?"; if (path.IndexOf("?") >= 0) { path = "&"; } parameterUrl += FormatParameters(parameters); if (contentType == ContentTypeEnum.URL_ENCODED) { parameterUrl = UriHelper.UrlEncode(parameterUrl); } httpItem.URL += parameterUrl; } else { #region POST 数据处理 Encoding encodingItem = Encoding.GetEncoding(encoding); httpItem.PostDataType = PostDataType.Byte; httpItem.PostEncoding = encodingItem; if (contentType == ContentTypeEnum.URL_ENCODED) { httpItem.PostdataByte = encodingItem.GetBytes(parameters is string?parameters.ToString() : FormatParameters(parameters)); } else if (contentType == ContentTypeEnum.JSON) { httpItem.PostdataByte = encodingItem.GetBytes(parameters is string?parameters.ToString() : JsonHelper.ToJson(parameters)); } else if (contentType == ContentTypeEnum.FORM_DATA) { Dictionary <string, string> filePathDict = new Dictionary <string, string>(); string parameterData = FormatParameters(parameters, filePathDict); List <byte> postDataByteList = new List <byte>(); // 添加其他数据 postDataByteList.AddRange(encodingItem.GetBytes(parameterData)); // 如果存在文件数据 if (filePathDict != null && filePathDict.Count > 0) { foreach (KeyValuePair <string, string> keyValueItem in filePathDict) { if (!string.IsNullOrEmpty(keyValueItem.Value)) { using (StreamReader streamReader = new StreamReader(keyValueItem.Value, encodingItem)) { postDataByteList.AddRange(encodingItem.GetBytes(streamReader.ReadToEnd())); } } } } httpItem.PostdataByte = postDataByteList.ToArray(); } #endregion } } #endregion #region 设置头部数据 Dictionary <string, object> headerDict = CommonHelper.GetParameterDict(headers); if (headerDict != null && headerDict.Count > 0) { foreach (KeyValuePair <string, object> keyValueItem in headerDict) { httpItem.Header.Add(keyValueItem.Key, keyValueItem.Value != null ? keyValueItem.Value.ToString() : ""); } } #endregion if (customFun != null) { httpItem = customFun(httpItem); } return(httpHelper.GetHtml(httpItem)); }
/// <summary> /// 获取 Cookie 数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="cookieName">Cookie 名称</param> /// <param name="httpRequest">HttpRequestBase,如果未指定则取自 HttpContext.Current.Request</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static T GetCookieT <T>(string cookieName, HttpRequestBase httpRequest = null, object propertyMatchList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(propertyMatchList); return(ExecuteGetCookie <T>(httpRequest, cookieName, propertyDict, reflectionType)); }
/// <summary> /// 按模板生成 Word 文档 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="templatePath">模板路径</param> /// <param name="wordPath">Word 路径</param> /// <param name="dataMatchList">数据匹配,Dictionary<string, object> 或 new {}</param> public static void ToWord(string templatePath, string wordPath, object dataMatchList) { FileStream fileStream = null; XWPFDocument document = null; try { using (fileStream = File.OpenRead(templatePath)) { document = new XWPFDocument(fileStream); } IList <XWPFTableRow> rowItemList = null; IList <XWPFTableCell> cellItemList = null; IList <XWPFTable> tableItemList = document.Tables; List <XWPFParagraph> paragraphItemList = new List <XWPFParagraph>(); paragraphItemList.AddRange(document.Paragraphs); #region 处理表格数据 if (tableItemList != null && tableItemList.Count > 0) { foreach (XWPFTable tableItem in tableItemList) { rowItemList = tableItem.Rows; if (rowItemList != null && rowItemList.Count > 0) { foreach (XWPFTableRow rowItem in rowItemList) { cellItemList = rowItem.GetTableCells(); if (cellItemList != null && cellItemList.Count > 0) { foreach (XWPFTableCell cellItem in cellItemList) { paragraphItemList.AddRange(cellItem.Paragraphs); } } } } } } #endregion #region 处理段落数据 if (paragraphItemList != null && paragraphItemList.Count > 0) { Dictionary <string, object> dataDict = CommonHelper.GetParameterDict(dataMatchList); foreach (XWPFParagraph paragraph in paragraphItemList) { ExecuteReplaceParagraph(paragraph, dataDict); } } #endregion if (System.IO.File.Exists(wordPath)) { System.IO.File.Delete(wordPath); } using (fileStream = new FileStream(wordPath, FileMode.Create, FileAccess.Write)) { document.Write(fileStream); } } catch { throw; } finally { if (document != null) { document.Close(); } if (fileStream != null) { fileStream.Close(); } } }