26 lines
877 B
C#
26 lines
877 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MP.FileData
|
|
{
|
|
public class FileUtils
|
|
{
|
|
|
|
public static async Task SaveToCsv<T>(List<T> reportData, string path)
|
|
{
|
|
var lines = new List<string>();
|
|
IEnumerable<PropertyDescriptor> props = TypeDescriptor.GetProperties(typeof(T)).OfType<PropertyDescriptor>();
|
|
var header = string.Join(";", props.ToList().Select(x => x.Name));
|
|
lines.Add(header);
|
|
var valueLines = reportData.Select(row => string.Join(";", header.Split(';').Select(a => row.GetType().GetProperty(a).GetValue(row, null))));
|
|
lines.AddRange(valueLines);
|
|
await Task.Run(() => File.WriteAllLines(path, lines.ToArray()));
|
|
}
|
|
}
|
|
}
|