在CSV文件中写入C#对象列表

时间:2022-01-17 15:23:52

I have a C# object having 8 array elements of size 200.I need to print these arrays into a CSV file on respective labels. Data might contain string,int and double.

我有一个C#对象有8个大小为200的数组元素。我需要将这些数组打印到相应标签上的CSV文件中。数据可能包含string,int和double。

Ex:

例如:

time   time1   time2  Day  time4  time4 time5 time6 time7
1       5       9     Mon   7.0    8      9    5     NA
2
3
3
.
.
200    200    200    Sun   200    200   200   200    200

Oops, time1 etc are labels(Header) the data(8 lists having 200 elements) should write under these labels. Appreciate your response !

哎呀,time1等是标签(Header)数据(8个列表有200个元素)应该在这些标签下写入。感谢您的回复!

2 个解决方案

#1


28  

You could write a generic function to write the objects:

您可以编写一个通用函数来编写对象:

public void WriteCSV<T>(IEnumerable<T> items, string path)
{
  Type itemType = typeof(T);
  var props = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                      .OrderBy(p => p.Name);

  using (var writer = new StreamWriter(path))
  {
    writer.WriteLine(string.Join(", ", props.Select(p => p.Name)));

    foreach (var item in items)
    {
      writer.WriteLine(string.Join(", ", props.Select(p => p.GetValue(item, null))));
    }
  }
}

Used as:

用作:

var people = new List<Person> { new Person("Matt", "Abbott"), new Person("John Smith") };
WriteCSV(people, @"C:\people.csv");

Which might output:

可能输出:

Forename, Surname
Matt", Abbott"
John", Smith"

#2


3  

Assuming none of your data needs to have comma escaping, this should give you a general idea:

假设您的数据都不需要逗号转义,这应该给您一个大致的想法:

string[][] myArray = // your data
string[] myHeaders = // your headers
File.WriteAllText("somefile.csv", 
        string.Join(Environment.NewLine, 
            new[]{myHeaders}.Concat(myArray)
            .Select(line => string.Join(",", line))));

#1


28  

You could write a generic function to write the objects:

您可以编写一个通用函数来编写对象:

public void WriteCSV<T>(IEnumerable<T> items, string path)
{
  Type itemType = typeof(T);
  var props = itemType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                      .OrderBy(p => p.Name);

  using (var writer = new StreamWriter(path))
  {
    writer.WriteLine(string.Join(", ", props.Select(p => p.Name)));

    foreach (var item in items)
    {
      writer.WriteLine(string.Join(", ", props.Select(p => p.GetValue(item, null))));
    }
  }
}

Used as:

用作:

var people = new List<Person> { new Person("Matt", "Abbott"), new Person("John Smith") };
WriteCSV(people, @"C:\people.csv");

Which might output:

可能输出:

Forename, Surname
Matt", Abbott"
John", Smith"

#2


3  

Assuming none of your data needs to have comma escaping, this should give you a general idea:

假设您的数据都不需要逗号转义,这应该给您一个大致的想法:

string[][] myArray = // your data
string[] myHeaders = // your headers
File.WriteAllText("somefile.csv", 
        string.Join(Environment.NewLine, 
            new[]{myHeaders}.Concat(myArray)
            .Select(line => string.Join(",", line))));

相关文章