using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace BFF.ExportData { public class ExportFile { #region Export public static MemoryStream StreamToCSV<T>(IEnumerable<T> dataSource, string fileName) where T : class { var result = string.Empty; //Header StringBuilder headerRow = new StringBuilder(); var properties = typeof(T).GetProperties().Select(p => new { PropertyInfo = p, HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute }) .Where(p => p.HeaderAttr != null) .OrderBy(p => p.HeaderAttr.Order) .ThenBy(p => p.PropertyInfo.Name) .ToList(); var propertiesCount = properties.Count(); for (int i = 0; i < propertiesCount; i++) { if (i < propertiesCount - 1) headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(","); else headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine); } //Data StringBuilder fields = new StringBuilder(); foreach (var item in dataSource) { if (item == null) continue; for (int i = 0; i < propertiesCount; i++) { var propertyValue = properties[i].PropertyInfo.GetValue(item, null); var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString(); if (i < propertiesCount - 1) formatStringToCSVForm(fields, displayPropertyValue, false); else formatStringToCSVForm(fields, displayPropertyValue, true); } fields.Append(Environment.NewLine); } //build result = headerRow.ToString() + fields.ToString(); //return result; byte[] bytetxt = Encoding.UTF8.GetBytes(result); MemoryStream memstream = new MemoryStream(); memstream.Write(bytetxt, 0, bytetxt.Length); memstream.Seek(0, SeekOrigin.Begin); return memstream; } public static void ExportToCSV<T>(IEnumerable<T> dataSource, string fileName) where T : class { var result = string.Empty; //Header StringBuilder headerRow = new StringBuilder(); var properties = typeof(T).GetProperties().Select(p => new { PropertyInfo = p, HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute }) .Where(p => p.HeaderAttr != null) .OrderBy(p => p.HeaderAttr.Order) .ThenBy(p => p.PropertyInfo.Name) .ToList(); var propertiesCount = properties.Count(); for (int i = 0; i < propertiesCount; i++) { if (i < propertiesCount - 1) headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(","); else headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine); } //Data StringBuilder fields = new StringBuilder(); foreach (var item in dataSource) { if (item == null) continue; for (int i = 0; i < propertiesCount; i++) { var propertyValue = properties[i].PropertyInfo.GetValue(item, null); var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString(); if (i < propertiesCount - 1) formatStringToCSVForm(fields, displayPropertyValue, false); else formatStringToCSVForm(fields, displayPropertyValue, true); } fields.Append(Environment.NewLine); } //build result = headerRow.ToString() + fields.ToString(); //return result; byte[] bytetxt = Encoding.UTF8.GetBytes(result); FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); f.Write(bytetxt,0,bytetxt.Length); f.Close(); } private static void formatStringToCSVForm(StringBuilder sb, string field, bool isLast) { if (string.IsNullOrEmpty(field)) sb.Append(","); else { sb.AppendFormat(""{0}"", field.Replace(""", """")); if (!isLast) sb.Append(","); } } #endregion } }