📜  对象列表到csv c#代码示例

📅  最后修改于: 2022-03-11 14:49:11.609000             🧑  作者: Mango

代码示例1
private void SaveToCsv(List reportData, string path)
{
    var lines = new List();
    IEnumerable props = TypeDescriptor.GetProperties(typeof(T)).OfType();
    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);
    File.WriteAllLines(path, lines.ToArray());
}