internal static void ToSheet <T>(Workbook workbook, List <T> dataList, string sheetName, List <string> filterNameList = null, bool propertyContain = true, Dictionary <string, object> propertyDict = null, Dictionary <string, object> valueFormatDict = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Worksheet iSheet = workbook.Worksheets[0]; iSheet.Name = sheetName; // 获得表头数据 Dictionary <string, PropertyInfo> headerColumnNameDict = CommonHelper.InitPropertyWriteMapperFormat <T, ExcelTAttribute>(propertyDict, filterNameList, propertyContain); Cells cellList = iSheet.Cells; int columnIndex = 0; // 遍历设置表头 foreach (KeyValuePair <string, PropertyInfo> keyValuePair in headerColumnNameDict) { cellList[0, columnIndex].PutValue(keyValuePair.Key); columnIndex++; } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } if (dataList != null) { // 遍历设置数据 for (int rowIndex = 1; rowIndex <= dataList.Count; rowIndex++) { SetRowDataValue(cellList, rowIndex, dataList[rowIndex - 1], propertyGetDict, headerColumnNameDict, valueFormatDict); } } }
internal static DataTable ExecuteToDataTable <T>(List <T> dataList, Dictionary <string, object> propertyDict, List <string> propertyList = null, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { Dictionary <string, string> columnNameList = CommonHelper.InitPropertyWriteMapper <T, DataTableTAttribute>(propertyDict, propertyList, propertyContain); DataTable dataTable = new DataTable(); // 遍历设置标题 foreach (KeyValuePair <string, string> keyValuePair in columnNameList) { dataTable.Columns.Add(keyValuePair.Key); } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } DataRow dataRow = null; // 遍历数据并设置到 DataTable foreach (T t in dataList) { dataRow = dataTable.NewRow(); SetRowDataValue(dataRow, t, propertyGetDict, columnNameList); dataTable.Rows.Add(dataRow); } return(dataTable); }
/// <summary> /// 复制数据 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="originalData">原实体数据</param> /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param> /// <param name="propertyIgnore">忽略属性列表,例:new { ID = "" }</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static T Copy <T>(object originalData, object propertyMatchList = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), typeof(T), propertyMatchList, propertyIgnore); dynamic propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalData.GetType(), reflectionType); dynamic propertySetDict = ReflectionExtendHelper.PropertySetCallDict <T>(reflectionType); return(CopyData(typeof(T), propertySetDict, propertyGetDict, propertyMatchDict, originalData, propertyMatchList, propertyIgnore, reflectionType) as T); }
/// <summary> /// 复制数据 /// </summary> /// <param name="originalData">原实体数据</param> /// <param name="targetData">目标实体数据</param> /// <param name="propertyMatchList">属性匹配,例:new { ID = "UserID" }</param> /// <param name="propertyIgnore">忽略属性列表,例:new { ID = "" }</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static dynamic Copy(object originalData, object targetData, object propertyMatchList = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { Dictionary <PropertyInfo, string> propertyMatchDict = InitPropertyMatchMapper(originalData.GetType(), targetData.GetType(), propertyMatchList, propertyIgnore); dynamic propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalData.GetType(), reflectionType); dynamic propertySetDict = ReflectionExtendHelper.PropertySetCallDict(targetData.GetType(), reflectionType); object copyData = CopyData(targetData, propertySetDict, propertyGetDict, propertyMatchDict, originalData, propertyMatchList, propertyIgnore, reflectionType); return(Convert.ChangeType(copyData, targetData.GetType())); }
internal static void ToSheet <T>(IWorkbook workbook, List <T> dataList, string sheetName, Action <ICell, object> cellCallback = null, Action <ISheet, List <string> > sheetCallback = null, bool isHeader = true, List <string> filterNameList = null, bool propertyContain = true, Dictionary <string, object> propertyDict = null, Dictionary <string, object> valueFormatDict = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { ISheet iSheet = workbook.CreateSheet(sheetName); // 获得表头数据 Dictionary <string, PropertyInfo> headerColumnNameDict = CommonHelper.InitPropertyWriteMapperFormat <T, ExcelTAttribute>(propertyDict, filterNameList, propertyContain); int dataIndex = 0; if (isHeader) { IRow headerRow = iSheet.CreateRow(dataIndex); int columnIndex = 0; // 遍历设置表头 foreach (KeyValuePair <string, PropertyInfo> keyValuePair in headerColumnNameDict) { headerRow.CreateCell(columnIndex).SetCellValue(keyValuePair.Key); columnIndex++; } dataIndex = 1; } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } IRow row = null; if (dataList != null) { // 遍历设置数据 for (int rowIndex = 0; rowIndex < dataList.Count; rowIndex++, dataIndex++) { row = iSheet.CreateRow(dataIndex); SetRowDataValue(row, cellCallback, dataList[rowIndex], propertyGetDict, headerColumnNameDict, valueFormatDict); } } if (sheetCallback != null) { sheetCallback(iSheet, headerColumnNameDict.Keys.ToList()); } }
internal static void ExecuteSetCookie <T>(HttpResponseBase httpResponse, string cookieName, T data, DateTime expires, Dictionary <string, object> propertyDict, List <string> propertyList, bool propertyContain = true, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { Dictionary <string, string> cookieNameDict = CommonHelper.InitPropertyWriteMapper <T, CookieTAttribute>(propertyDict, propertyList, propertyContain); dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } HttpCookie httpCookie = new HttpCookie(cookieName); httpCookie.HttpOnly = true; httpCookie.Expires = expires; object cookieValue = null; foreach (KeyValuePair <string, string> keyValueItem in cookieNameDict) { if (propertyGetDict != null && propertyGetDict.ContainsKey(keyValueItem.Value)) { cookieValue = propertyGetDict[keyValueItem.Value](data); } else { cookieValue = ReflectionHelper.GetPropertyValue(data, keyValueItem.Value); } if (cookieValue != null) { httpCookie.Values.Add(keyValueItem.Key, HttpUtility.UrlEncode(cookieValue.ToString())); } } if (httpResponse != null) { httpResponse.Cookies.Add(httpCookie); } else { HttpContext.Current.Response.Cookies.Add(httpCookie); } }
private static void ExecuteSetValue <T>(Action <List <string>, dynamic, XmlNode, string> callback, string xmlPath, string xPath, XmlDocument xmlDocument, string[] propertyList = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { List <string> filterNameList = null; if (propertyList != null && propertyList.Length > 0) { filterNameList = propertyList.ToList(); } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } XmlNode xmlNode = GetNode(xPath, xmlDocument); if (xmlNode == null) { return; } string elementName = ReflectionExtendHelper.GetAttributeValue <XmlTAttribute>(typeof(T), p => p.Name); if (elementName == null) { elementName = typeof(T).Name; } if (callback != null) { callback(filterNameList, propertyGetDict, xmlNode, elementName); } if (!string.IsNullOrEmpty(xPath)) { xmlDocument.Save(xmlPath); } }
/// <summary> /// 实体对象数据写入 Txt /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="txtPath">Txt 路径</param> /// <param name="dataList">实体数据列表</param> /// <param name="splitChar">列分隔符,例:|</param> /// <param name="typeEnum">TxtTypeEnum 枚举类型</param> /// <param name="encoding">编码格式</param> /// <param name="propertyMatchList">属性匹配,Dictionary<string, object> 或 new {}</param> /// <param name="propertyList">属性列表,如果指定,则按指定属性列表生成文本数据</param> /// <param name="lineSplitChar">行分隔符,例:\r\n</param> /// <param name="reflectionType">反射类型</param> /// <returns></returns> public static bool ToTxt <T>(string txtPath, List <T> dataList, string splitChar, System.Text.Encoding encoding, TxtTypeEnum typeEnum, object propertyMatchList = null, string[] propertyList = null, bool propertyContain = true, string lineSplitChar = NewLine, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { bool result = ExecuteStreamWriter(txtPath, encoding, false, () => { StringBuilder stringBuilder = new StringBuilder(); Dictionary <int, PropertyInfo> propertyNameDict = InitEntityToTxtMapper <T>(propertyMatchList, typeEnum, propertyList, propertyContain); List <int> propertyIndexList = propertyNameDict.Keys.ToList <int>(); propertyIndexList.Sort((int x, int y) => { if (x > y) { return(1); } if (x < y) { return(-1); } return(0); }); dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } foreach (T t in dataList) { stringBuilder.Append(EntityToText <T>(t, splitChar, propertyGetDict, propertyNameDict, propertyIndexList, lineSplitChar)); } return(stringBuilder.ToString()); }); return(result); }
private static object CopyData(object targetData, dynamic propertySetDict, dynamic propertyGetDict, Dictionary <PropertyInfo, string> propertyMatchDict, dynamic originalData, object propertyMatch = null, object propertyIgnore = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) { if (originalData == null) { return(targetData); } PropertyInfo propertyInfo = null; dynamic propertyValue = null; foreach (var keyValueItem in propertyMatchDict) { propertyInfo = keyValueItem.Key; if (propertyGetDict != null && propertyGetDict.ContainsKey(keyValueItem.Value)) { propertyValue = propertyGetDict[keyValueItem.Value](originalData); } else { propertyValue = ReflectionHelper.GetPropertyValue(originalData, keyValueItem.Value); } if (propertyValue != null) { if (ReflectionHelper.IsListType(propertyInfo.PropertyType)) { Type originalTType = ReflectionHelper.GetListGenericType(originalData.GetType().GetProperty(keyValueItem.Value)); var originalPropertyMatchValue = ReflectionHelper.GetPropertyValue(propertyMatch, propertyInfo.Name); var originalPropertyIgnoreValue = ReflectionHelper.GetPropertyValue(propertyIgnore, propertyInfo.Name); var originalPropertyValue = ReflectionHelper.GetPropertyValue(targetData, propertyInfo) as IList; Type listTType = ReflectionHelper.GetListGenericType(propertyInfo); var objectDataList = ReflectionHelper.NewList(listTType) as IList; dynamic listPropertySetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { listPropertySetDict = ReflectionExtendHelper.PropertySetCallDict(listTType, reflectionType); } dynamic listPropertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { listPropertyGetDict = ReflectionExtendHelper.PropertyGetCallDict(originalTType, reflectionType); } for (int index = 0; index < propertyValue.Count; index++) { if (originalPropertyValue != null && index < originalPropertyValue.Count) { objectDataList.Add(CopyData(originalPropertyValue[index], listPropertySetDict, listPropertyGetDict, InitPropertyMatchMapper(originalTType, listTType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue[index], originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType)); } else { objectDataList.Add(CopyData(listTType, listPropertySetDict, listPropertyGetDict, InitPropertyMatchMapper(originalTType, listTType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue[index], originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType)); } } targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, objectDataList, null); } else if (ReflectionHelper.IsCustomType(propertyInfo.PropertyType)) { var originalPropertyMatchValue = ReflectionHelper.GetPropertyValue(propertyMatch, propertyInfo.Name); var originalPropertyIgnoreValue = ReflectionHelper.GetPropertyValue(propertyIgnore, propertyInfo.Name); var originalPropertyValue = ReflectionHelper.GetPropertyValue(targetData, propertyInfo); if (originalPropertyValue == null) { targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, CopyData(propertyInfo.PropertyType, null, null, InitPropertyMatchMapper(propertyValue.GetType(), propertyInfo.PropertyType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue, originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType), null); } else { targetData.GetType().GetProperty(propertyInfo.Name).SetValue(targetData, CopyData(originalPropertyValue, null, null, InitPropertyMatchMapper(propertyValue.GetType(), propertyInfo.PropertyType, originalPropertyMatchValue, originalPropertyIgnoreValue), propertyValue, originalPropertyMatchValue, originalPropertyIgnoreValue, reflectionType), null); } } else { if (propertySetDict != null && propertySetDict.ContainsKey(propertyInfo.Name)) { ReflectionGenericHelper.SetPropertyValue(propertySetDict[propertyInfo.Name], targetData, propertyValue.ToString(), propertyInfo); } else { ReflectionHelper.SetPropertyValue(targetData, propertyValue.ToString(), propertyInfo); } } } } return(targetData); }
/// <summary> /// 按模板生成 Excel 文档 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="templatePath">模板路径</param> /// <param name="templateSheetName">模板表单名称</param> /// <param name="excelPath">Excel 路径</param> /// <param name="dataMatchList">数据匹配,Dictionary<string, object> 或 new {}</param> /// <param name="dataList">数据列表</param> /// <param name="reflectionType">反射类型</param> public static void ToExcel <T>(string templatePath, string templateSheetName, string excelPath, object dataMatchList, List <T> dataList, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { Dictionary <string, object> propertyDict = CommonHelper.GetParameterDict(dataMatchList); FileStream fileStream = null; IWorkbook workbook = null; try { using (fileStream = new FileStream(templatePath, FileMode.Open, FileAccess.ReadWrite)) { workbook = ExcelHelper.ExecuteIWorkBookGet(templatePath, fileStream); if (workbook == null) { throw new Exception(ExcelHelper.ExcelWorkbookNullException); } } Dictionary <int, string> propertyMapperDict = new Dictionary <int, string>(); List <ExcelTemplateFormulaItem> formulaItemList = new List <ExcelTemplateFormulaItem>(); int lastRowIndex = 0; int insertRowIndex = -1; ISheet sheet = workbook.GetSheet(templateSheetName); if (sheet != null) { lastRowIndex = sheet.LastRowNum; for (int rowIndex = 0; rowIndex <= lastRowIndex; rowIndex++) { IRow iRow = sheet.GetRow(rowIndex); if (iRow != null) { for (int colIndex = 0; colIndex <= iRow.LastCellNum; colIndex++) { ICell iCell = iRow.GetCell(colIndex); if (iCell != null && !string.IsNullOrEmpty(iCell.ToString())) { string cellText = iCell.ToString(); if (cellText.StartsWith("$")) { cellText = cellText.TrimStart(new char[] { '$' }); if (propertyDict != null && propertyDict.ContainsKey(cellText)) { iCell.SetCellValue(propertyDict[cellText].ToString()); } } else if (cellText.StartsWith("#")) { if (insertRowIndex == -1) { insertRowIndex = rowIndex; } cellText = cellText.TrimStart(new char[] { '#' }); propertyMapperDict.Add(colIndex, cellText); } else if (cellText.StartsWith("&=")) { if (rowIndex != insertRowIndex) { formulaItemList.Add(new ExcelTemplateFormulaItem() { Cell = iCell, FormulaText = cellText }); } } } } } } } if (propertyMapperDict != null && propertyMapperDict.Count > 0 && insertRowIndex != -1) { if (dataList != null && dataList.Count > 0) { dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } CopyRow(sheet, insertRowIndex, dataList, propertyMapperDict, propertyGetDict); if (formulaItemList != null && formulaItemList.Count > 0) { foreach (ExcelTemplateFormulaItem formulaItem in formulaItemList) { formulaItem.FormulaText = formulaItem.FormulaText.TrimStart(new char[] { '&', '=' }); formulaItem.FormulaText = formulaItem.FormulaText.Replace("_dataBegin", (insertRowIndex + 1).ToString()); formulaItem.FormulaText = formulaItem.FormulaText.Replace("_dataEnd", (insertRowIndex + dataList.Count).ToString()); formulaItem.Cell.SetCellFormula(formulaItem.FormulaText); formulaItem.Cell.SetCellType(CellType.Formula); } } } } sheet.ForceFormulaRecalculation = true; if (System.IO.File.Exists(excelPath)) { System.IO.File.Delete(excelPath); } using (fileStream = new FileStream(excelPath, FileMode.Create, FileAccess.ReadWrite)) { workbook.Write(fileStream); } workbook.Close(); } catch { throw; } finally { if (fileStream != null) { fileStream.Close(); } if (workbook != null) { workbook.Close(); } } }
private static bool ExecuteSet <T>(List <T> dataList, string primaryKey = "", List <string> propertyList = null, int maxBufferedDocs = 1000, int maxMergeFactory = 1000, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class { IndexWriter indexWriter = null; IndexWriter RAMWriter = null; try { RAMDirectory = new RAMDirectory(); bool create = IndexReader.IndexExists(IndexDictDirectory); if (create) { if (IndexWriter.IsLocked(IndexDictDirectory)) { IndexWriter.Unlock(IndexDictDirectory); } } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } indexWriter = new IndexWriter(IndexDictDirectory, new PanGuAnalyzer(), !create, IndexWriter.MaxFieldLength.LIMITED); RAMWriter = new IndexWriter(RAMDirectory, new PanGuAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); RAMWriter.SetMaxBufferedDocs(maxBufferedDocs); RAMWriter.SetMergeFactor(maxMergeFactory); List <List <LuceneIndexModel> > modelList = EntityListToLuceneIndexModelList(dataList, propertyGetDict, propertyList); Document document = null; Term term = null; string primaryValue = null; foreach (List <LuceneIndexModel> model in modelList) { document = new Document(); foreach (LuceneIndexModel fieldModel in model) { if (fieldModel.Name == primaryKey) { primaryValue = fieldModel.Value; } document.Add(new Field(fieldModel.Name, fieldModel.Value, fieldModel.StoreEnum, fieldModel.IndexEnum)); } if (string.IsNullOrEmpty(primaryKey)) { RAMWriter.AddDocument(document); } else { term = new Term(primaryKey, primaryValue); RAMWriter.UpdateDocument(term, document); } } RAMWriter.Close(); indexWriter.AddIndexesNoOptimize(new Directory[] { RAMDirectory }); indexWriter.Optimize(); indexWriter.Close(); return(true); } catch { throw; } finally { if (indexWriter != null) { indexWriter.Close(); } } }