public static void CreateCsv <T>(string path, string name, IEnumerable <T> objects, LogController ui)
        {
            name = GetCsvFileName(name);

            if (objects != null && objects.Any(o => o != null))
            {
                var typeToOutput = objects.First(o => o != null).GetType();

                var propertyNames     = typeToOutput.GetReadableProperties().Select(p => p.Name).ToArray();
                var propertyNamesText = String.Join(",", propertyNames);
                FileUtility.CheckCreateFile(path, name, propertyNamesText);

                var stringBuilder = new StringBuilder();
                stringBuilder.AppendLine();

                foreach (var o in objects)
                {
                    var thisObjectType = o.GetType();
                    var valueStrings   = new List <string>();
                    foreach (var property in propertyNames)
                    {
                        object value = null;
                        if (thisObjectType.GetProperty(property) != null)
                        {
                            value = o.GetPropertyValue(property);
                        }
                        if (value == null)
                        {
                            valueStrings.Add("");
                        }
                        else
                        {
                            var thisString = value.ToString();
                            thisString = thisString.Replace("\n", CsvNewLine);
                            thisString = thisString.Replace("\"", "\"\"");
                            valueStrings.Add("\"" + thisString + "\"");
                        }
                    }
                    stringBuilder.AppendLine(string.Join(",", valueStrings));
                }
                FileUtility.AppendToFile(path, name, stringBuilder.ToString());
            }
        }
Exemplo n.º 2
0
 public abstract void ExecuteExtention(TRequest request, TResponse response, LogController controller);