private void SetGenericTypePropertiesMode(Type genericType, ref string mode, out int maxColumnIndex, out List <ExcelColumnInfo> props) { mode = "Properties"; if (genericType.IsValueType) { throw new NotImplementedException($"MiniExcel not support only {genericType.Name} value generic type"); } else if (genericType == typeof(string) || genericType == typeof(DateTime) || genericType == typeof(Guid)) { throw new NotImplementedException($"MiniExcel not support only {genericType.Name} generic type"); } props = CustomPropertyHelper.GetSaveAsProperties(genericType, _configuration); maxColumnIndex = props.Count; }
public void SaveAs() { var seperator = _configuration.Seperator.ToString(); var newLine = _configuration.NewLine; using (StreamWriter writer = _configuration.StreamWriterFunc(_stream)) { if (_value == null) { writer.Write(""); return; } var type = _value.GetType(); Type genericType = null; if (_value is IDataReader) { GenerateSheetByIDataReader(_value, seperator, newLine, writer); } else if (_value is IEnumerable) { var values = _value as IEnumerable; List <object> keys = new List <object>(); List <ExcelColumnInfo> props = null; string mode = null; // check mode { foreach (var item in values) //TODO: need to optimize { if (item != null && mode == null) { if (item is IDictionary <string, object> ) { var item2 = item as IDictionary <string, object>; mode = "IDictionary<string, object>"; foreach (var key in item2.Keys) { keys.Add(key); } } else if (item is IDictionary) { var item2 = item as IDictionary; mode = "IDictionary"; foreach (var key in item2.Keys) { keys.Add(key); } } else { mode = "Properties"; genericType = item.GetType(); props = CustomPropertyHelper.GetSaveAsProperties(genericType, _configuration); } break; } } } //if(mode == null) // throw new NotImplementedException($"Type {type?.Name} & genericType {genericType?.Name} not Implemented. please issue for me."); if (keys.Count == 0 && props == null) { writer.Write(newLine); return; } if (this._printHeader) { if (props != null) { writer.Write(string.Join(seperator, props.Select(s => CsvHelpers.ConvertToCsvValue(s?.ExcelColumnName)))); writer.Write(newLine); } else if (keys.Count > 0) { writer.Write(string.Join(seperator, keys)); writer.Write(newLine); } else { throw new InvalidOperationException("Please issue for me."); } } if (mode == "IDictionary<string, object>") //Dapper Row { GenerateSheetByDapperRow(writer, _value as IEnumerable, keys.Cast <string>().ToList(), seperator, newLine); } else if (mode == "IDictionary") //IDictionary { GenerateSheetByIDictionary(writer, _value as IEnumerable, keys, seperator, newLine); } else if (mode == "Properties") { GenerateSheetByProperties(writer, _value as IEnumerable, props, seperator, newLine); } else { throw new NotImplementedException($"Type {type?.Name} & genericType {genericType?.Name} not Implemented. please issue for me."); } } else if (_value is DataTable) { GenerateSheetByDataTable(writer, _value as DataTable, seperator, newLine); } else { throw new NotImplementedException($"Type {type?.Name} & genericType {genericType?.Name} not Implemented. please issue for me."); } } }