/// <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); }
public void Serialize(Type itemType, object value, IXlsxDocumentBuilder document, string sheetName = null, string columnPrefix = null, XlsxExportImport.Base.Builders.SqadXlsxSheetBuilder sheetBuilderOverride = null) { ExcelColumnInfoCollection columnInfo = _columnResolver.GetExcelColumnInfo(itemType, value, sheetName); XlsxExportImport.Base.Builders.SqadXlsxSheetBuilder sheetBuilder = null; if (sheetName == null) { var sheetAttribute = itemType.GetCustomAttributes(true).SingleOrDefault(s => s is ExcelSheetAttribute); sheetName = sheetAttribute != null ? (sheetAttribute as ExcelSheetAttribute).SheetName : itemType.Name; } if (columnInfo.Any()) { if (sheetBuilderOverride == null) { sheetBuilder = document.GetSheetByName(sheetName) as XlsxExportImport.Base.Builders.SqadXlsxSheetBuilder; } else { sheetBuilder = sheetBuilderOverride; } if (sheetBuilder == null) { sheetBuilder = new XlsxExportImport.Base.Builders.SqadXlsxSheetBuilder(sheetName); //Move this to attribute hidden property //if (new List<string>() { "Formulas", "LeftTableColumn", "Cells" }.Contains(sheetName)) //{ // sheetBuilder.IsHidden = true; //} document.AppendSheet(sheetBuilder); } //Convert Dictionary Column foreach (var col in columnInfo) { if (col.PropertyName.EndsWith("_Dict_")) { string columnName = col.PropertyName.Replace("_Dict_", ""); object colValueDict = null; if (sheetName == col.PropertyName.Replace("_Dict_", "")) { colValueDict = value; } else { colValueDict = GetFieldOrPropertyValue(value, col.PropertyName.Replace("_Dict_", "")); } if (columnName.Contains(":") && (colValueDict == null || (colValueDict != null && string.IsNullOrEmpty(colValueDict.ToString())))) { colValueDict = GetFieldPathValue(value, columnName); } if (colValueDict == null || string.IsNullOrEmpty(colValueDict.ToString())) { continue; } object dictionaryKeys = colValueDict.GetType().GetProperty("Keys").GetValue(colValueDict); int count = 0; foreach (var key in (System.Collections.IEnumerable)dictionaryKeys) { ExcelColumnInfo temlKeyColumn = col.Clone() as ExcelColumnInfo; temlKeyColumn.PropertyName = temlKeyColumn.PropertyName.Replace("_Dict_", $":Key:{count}"); sheetBuilder.AppendColumnHeaderRowItem(temlKeyColumn); var currentItem = colValueDict.GetType().GetProperty("Item").GetValue(colValueDict, new object[] { key }); if (FormatterUtils.IsSimpleType(currentItem.GetType())) { ExcelColumnInfo temlValueColumn = col.Clone() as ExcelColumnInfo; temlValueColumn.PropertyName = temlValueColumn.PropertyName.Replace("_Dict_", $":Value:{count}"); sheetBuilder.AppendColumnHeaderRowItem(temlValueColumn); } else { string path = col.PropertyName.Replace("_Dict_", $":Value:{count}"); this.Serialize(currentItem.GetType(), value, document, sheetName, path, sheetBuilderOverride); } count++; } } else if (col.PropertyName.EndsWith("_List_")) { string columnName = col.PropertyName.Replace("_List_", ""); List <object> colListValue = GetFieldOrPropertyValue(value, col.PropertyName.Replace("_List_", "")) as List <object>; if (columnName.Contains(":") && (colListValue == null || (colListValue != null && string.IsNullOrEmpty(colListValue.ToString())))) { colListValue = GetFieldPathValue(value, columnName) as List <object>; } if (colListValue == null) { continue; } int dictColumnCount = colListValue.Count(); for (int i = 0; i < dictColumnCount; i++) { string listColumnPrefix = col.PropertyName.Replace("_List_", $":{i}"); if (FormatterUtils.IsSimpleType(colListValue[i].GetType())) { ExcelColumnInfo colToAppend = (ExcelColumnInfo)col.Clone(); colToAppend.PropertyName = listColumnPrefix; sheetBuilder.AppendColumnHeaderRowItem(colToAppend); } else { this.Serialize(colListValue[i].GetType(), colListValue[i], document, null, listColumnPrefix, sheetBuilder); } } } else if (col.PropertyName.EndsWith("_CustomField_") || col.PropertyName.EndsWith("_CustomField_Single_")) { string columnName = col.PropertyName.Replace("_CustomField_", "").Replace("Single_", ""); List <object> colCustomFields = GetFieldOrPropertyValue(value, columnName) as List <object>; if (columnName.Contains(":") && (colCustomFields == null || (colCustomFields != null && string.IsNullOrEmpty(colCustomFields.ToString())))) { colCustomFields = GetFieldPathValue(value, columnName) as List <object>; } if (colCustomFields == null) { continue; } foreach (var customField in colCustomFields) { int customFieldId = ((dynamic)customField).ID; bool isActual = ((dynamic)customField).Actual; ExcelColumnInfo temlKeyColumn = col.Clone() as ExcelColumnInfo; string propetyActual = isActual ? ":Actual" : string.Empty; string customFieldDef = _staticValuesResolver.GetCustomFieldName(customFieldId); if (col.PropertyName.EndsWith("_CustomField_Single_")) { customFieldDef = string.Empty; temlKeyColumn.PropertyName = temlKeyColumn.PropertyName.Replace("_CustomField_Single_", $"{propetyActual}"); temlKeyColumn.ExcelColumnAttribute.Header = temlKeyColumn.Header = temlKeyColumn.PropertyName; } else { temlKeyColumn.PropertyName = temlKeyColumn.PropertyName.Replace("_CustomField_", $"{propetyActual}:{customFieldId}"); temlKeyColumn.ExcelColumnAttribute.Header = temlKeyColumn.Header = temlKeyColumn.PropertyName + ":" + customFieldDef; } sheetBuilder.AppendColumnHeaderRowItem(temlKeyColumn); } } else { if (columnPrefix != null) { ExcelColumnInfo temlKeyColumn = col.Clone() as ExcelColumnInfo; temlKeyColumn.PropertyName = $"{columnPrefix}:{temlKeyColumn.PropertyName}"; sheetBuilder.AppendColumnHeaderRowItem(temlKeyColumn); } else { sheetBuilder.AppendColumnHeaderRowItem(col); } } } } //if its recursive do not populate rows and return to parent if (columnPrefix != null) { return; } if (sheetName != null && sheetBuilder == null) { sheetBuilder = (XlsxExportImport.Base.Builders.SqadXlsxSheetBuilder)document.GetSheetByName(sheetName); } //adding rows data if (value != null) { var columns = columnInfo.Keys.ToList(); if (value is IEnumerable <object> && (value as IEnumerable <object>).Count() > 0) { foreach (var dataObj in value as IEnumerable <object> ) { PopulateRows(columns, dataObj, sheetBuilder, columnInfo, document); var deepSheetsInfo = _sheetResolver.GetExcelSheetInfo(itemType, dataObj); PopulateInnerObjectSheets(deepSheetsInfo, document, itemType); } } else if (!(value is IEnumerable <object>)) { PopulateRows(columns, value, sheetBuilder, columnInfo, document); var sheetsInfo = _sheetResolver.GetExcelSheetInfo(itemType, value); PopulateInnerObjectSheets(sheetsInfo, document, itemType); } } if (sheetBuilder != null) { sheetBuilder.ShouldAddHeaderRow = true; } }