public void LoadComplexProperty(object source, string tagID) { ExceptionHelper.TrueThrow(source == null, "源对象不能为空"); DCTWordDataObject wordDataObj = new DCTWordDataObject(); DCTComplexProperty complexProp = this.PropertyCollection[tagID] as DCTComplexProperty; if (null == complexProp) { complexProp = new DCTComplexProperty(); complexProp.TagID = tagID; this.PropertyCollection.Add(complexProp); } Type sourceType = source.GetType(); PropertyInfo[] fieldInfos = sourceType.GetProperties(); foreach (PropertyInfo propInfo in fieldInfos) { if (propInfo.PropertyType.Name != typeof(object).Name) { DCTSimpleProperty simpleProp = ConvertToSimpleProperty(propInfo, source); if (simpleProp != null) { wordDataObj.PropertyCollection.Add(simpleProp); } } } complexProp.DataObjects.Add(wordDataObj); }
public List <T> ToList <T>(string tagID) where T : new() { List <T> results = new List <T>(); Type type = typeof(T); PropertyInfo[] props = type.GetProperties(); DCTDataProperty dctDataProp = this.PropertyCollection[tagID]; ExceptionHelper.TrueThrow(dctDataProp == null || dctDataProp is DCTSimpleProperty, string.Format("无法根据{0}找到对应的DCTDataProperty", tagID)); DCTComplexProperty compProp = (DCTComplexProperty)dctDataProp; foreach (DCTWordDataObject wordObj in compProp.DataObjects) { T t = new T(); if (wordObj.PropertyCollection.Count == 0) { continue; } foreach (PropertyInfo prop in props) { object[] attrs = prop.GetCustomAttributes(typeof(WordPropertyAttribute), true); if (attrs.Length != 0) { WordPropertyAttribute attr = (WordPropertyAttribute)attrs[0]; DCTSimpleProperty simpleProp = (DCTSimpleProperty)wordObj.PropertyCollection[attr.TagID]; if (null == simpleProp) { continue; } string str = (string)((DCTSimpleProperty)wordObj.PropertyCollection[attr.TagID]).Value; prop.SetValue(t, TryParse(str, prop.PropertyType), null); } } results.Add(t); } return(results); }
public ComplexPropertyProcessor(WordprocessingDocument document, DCTComplexProperty dataProperty) : base(document, dataProperty) { }
private static void processDataAreas(WordprocessingDocument document, DCTWordDataObject wdo, List<int> ignoreControls) { var titleRows = document.MainDocumentPart.Document.Body.Descendants<TableRow>().Where(o => o.Descendants<BookmarkStart>().Any()); foreach (TableRow titleRow in titleRows) { var bookmarkStart = titleRow.Descendants<BookmarkStart>().FirstOrDefault(); if (bookmarkStart == null) continue; if (bookmarkStart.ColumnFirst == null) continue; //针对每一个区域,先读取表头的控件,同时将控件加入黑名单 DCTDataColumnCollection collection = new DCTDataColumnCollection(titleRow); ignoreControls.AddRange(collection.CoveredControlIds); //遍历区域中的每一行,生成复杂属性 var curRow = titleRow.NextSibling<TableRow>(); DCTComplexProperty cp = new DCTComplexProperty(); cp.TagID = bookmarkStart.Name; while (null != curRow && (!curRow.Descendants<BookmarkEnd>().Any(o => o.Id == bookmarkStart.Id))) { DCTWordDataObject childWdo = new DCTWordDataObject(); DCTDataRow newDataRow = DCTDataRow.FromTableRow(curRow, collection); if (!newDataRow.IsEmpty) childWdo.PropertyCollection.AddRange<DCTSimpleProperty>(newDataRow.ToSimpleProperties()); curRow = curRow.NextSibling<TableRow>(); cp.DataObjects.Add(childWdo); } wdo.PropertyCollection.Add(cp); } }