Wednesday, June 21, 2017

Export List To Excel

 public static void ExportToExcel(List<T52DataModel> myList)
        {
            string fileName = $"C:\\T52{DateTime.Now.Date.ToString("yyyyMMdd")}.xls";

            List<string> result = new List<string>();
            result.Add(String.Join(String.Empty, typeof(T52DataModel).GetProperties().Select(i => String.Format("{0}\t", i.Name)))); // Headers
            result.AddRange(myList.Select(i => String.Join("\t", i.GetType().GetProperties().Select(t => t.GetValue(i, null))))); // Lines

            File.WriteAllLines(fileName, result);

        }

No comments:

Post a Comment