public void GetAttribute_ExcelDocumentAttributeOfComplexTestItem_ExcelDocumentAttribute() { var complexTestItem = typeof(ComplexTestItem); var excelAttribute = FormatterUtils.GetAttribute <ExcelDocumentAttribute>(complexTestItem); Assert.IsNotNull(excelAttribute); Assert.AreEqual("Complex test item", excelAttribute.FileName); }
public void GetAttribute_ExcelColumnAttributeOfComplexTestItemValue2_ExcelColumnAttribute() { var value2 = typeof(ComplexTestItem).GetMember("Value2")[0]; var excelAttribute = FormatterUtils.GetAttribute <ExcelColumnAttribute>(value2); Assert.IsNotNull(excelAttribute); Assert.AreEqual(2, excelAttribute.Order); }
public void GetAttribute_ExcelDocumentAttributeOfComplexTestItem_ExcelDocumentAttribute() { var complexTestItem = typeof(ComplexTestItem); var excelAttribute = FormatterUtils.GetAttribute <ExcelDocumentAttribute>(complexTestItem); excelAttribute.Should().NotBeNull(); excelAttribute.FileName.Should().Be("Complex test item"); }
public void GetAttribute_ExcelColumnAttributeOfComplexTestItemValue2_ExcelColumnAttribute() { var value2 = typeof(ComplexTestItem).GetMember("Value2")[0]; var excelAttribute = FormatterUtils.GetAttribute <ExcelColumnAttribute>(value2); excelAttribute.Should().NotBeNull(); excelAttribute.Order.Should().Be(2); }
public ExcelSheetInfoCollection GetExcelSheetInfo(Type itemType, object data) { if (itemType.Name.StartsWith("Dictionary")) { return(null); } var sheets = FormatterUtils.GetMemberNames(itemType); var properties = GetSerialisablePropertyInfo(itemType); var sheetCollection = new ExcelSheetInfoCollection(); foreach (var sheet in sheets) { var prop = properties.FirstOrDefault(p => p.Name == sheet); ExcelSheetAttribute attr = FormatterUtils.GetAttribute <ExcelSheetAttribute>(prop); if (prop == null || attr == null) { continue; } var sheetInfo = new ExcelSheetInfo() { SheetType = prop.PropertyType, SheetName = sheet, ExcelSheetAttribute = attr, PropertyName = prop.Name, SheetObject = FormatterUtils.GetFieldOrPropertyValue(data, prop.Name) }; if (prop.PropertyType.Name.StartsWith("List") && sheetInfo.SheetObject != null) { sheetInfo.SheetType = FormatterUtils.GetEnumerableItemType(sheetInfo.SheetObject.GetType()); } sheetCollection.Add(sheetInfo); } return(sheetCollection); }
/// <summary> /// Get the <c>ExcelColumnInfo</c> for all members of a class. /// </summary> /// <param name="itemType">Type of item being serialised.</param> /// <param name="data">The collection of values being serialised. (Not used, provided for use by derived /// types.)</param> public virtual ExcelColumnInfoCollection GetExcelColumnInfo(Type itemType, object data, string namePrefix = "", bool isComplexColumn = false) { var fieldInfo = new ExcelColumnInfoCollection(); if (itemType.Name.StartsWith("Dictionary")) { var prefix = namePrefix + "_Dict_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, new ExcelColumnAttribute(), null)); return(fieldInfo); } var fields = GetSerialisableMemberNames(itemType, data); var properties = GetSerialisablePropertyInfo(itemType, data); // Instantiate field names and fieldInfo lists with serialisable members. foreach (var field in fields) { var prop = properties.FirstOrDefault(p => p.Name == field); if (prop == null) { continue; } Type propertyType = prop.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = propertyType.GetGenericArguments()[0]; } ExcelColumnAttribute attribute = FormatterUtils.GetAttribute <ExcelColumnAttribute>(prop); if (attribute != null) { string prefix = string.IsNullOrEmpty(namePrefix) == false ? $"{namePrefix}:{prop.Name}" : prop.Name; if (propertyType.Name.StartsWith("List")) { Type typeOfList = FormatterUtils.GetEnumerableItemType(propertyType); //if (FormatterUtils.IsSimpleType(typeOfList)) //{ // fieldInfo.Add(new ExcelColumnInfo(prefix, typeOfList, attribute, null)); //} //else if (typeOfList.FullName.EndsWith("CustomFieldModel") || typeOfList.Name.StartsWith("OverrideProperty")) { prefix += "_CustomField_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else { prefix += "_List_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); //ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(typeOfList, null, prefix, true); //foreach (var subcolumn in columnCollection) // fieldInfo.Add(subcolumn); } } else if (propertyType.Name.EndsWith("CustomFieldModel") || propertyType.Name.StartsWith("OverrideProperty")) { prefix += "_CustomField_Single_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else if (propertyType.Name.StartsWith("Dictionary")) { prefix += "_Dict_"; fieldInfo.Add(new ExcelColumnInfo(prefix, null, attribute, null)); } else if (!FormatterUtils.IsSimpleType(propertyType)) { ExcelColumnInfoCollection columnCollection = GetExcelColumnInfo(propertyType, null, prefix, true); foreach (var subcolumn in columnCollection) { fieldInfo.Add(subcolumn); } } else { string propertyName = isComplexColumn ? $"{namePrefix}:{field}" : field; string displayName = propertyName; if (attribute.DoNotUsePropertyName) { attribute.Header = namePrefix; } bool columnAlreadyadded = fieldInfo.Any(a => a.PropertyName == propertyName); if (!columnAlreadyadded) { if (FormatterUtils.IsExcelSupportedType(propertyType)) { fieldInfo.Add(new ExcelColumnInfo(propertyName, propertyType, attribute, null)); } else { fieldInfo.Add(new ExcelColumnInfo(propertyName, typeof(string), attribute, null)); } } } } } PopulateFieldInfoFromMetadata(fieldInfo, itemType, data); return(fieldInfo); }