简体   繁体   English

从对象列表 C# 创建 csv 文件

[英]Create a csv file from a list of objects C#

How can I create a csv file from a generic list of objects?如何从通用对象列表创建 csv 文件? In the example, a list of molecules, containing 3 objects of the type Molecule:在示例中,分子列表,包含 3 个分子类型的对象:

namespace example_reactioncalc
{

    class Program
    {
        public class Molecule
        {
            public double property_A { get; set; }
            public double property_B { get; set; }
        }

        public static Molecule Reaction(Molecule molecule_1, Molecule molecule_2)
        {
            Molecule reacted_molecule = new Molecule();
            reacted_molecule.property_A = molecule_1.property_A + molecule_2.property_A;
            reacted_molecule.property_B = (molecule_1.property_B + molecule_2.property_B) / 2;
            return reacted_molecule;
        }

        static void Main(string[] args)
        {
            // Initiation of the list of molecules
            List<Molecule> molecules = new List<Molecule>();

            // Adding two molecules to the list
            molecules.Add(new Molecule() { property_A = 10, property_B = 20 });
            molecules.Add(new Molecule() { property_A = 3, property_B = 7 });

            // Reacting two molecules to get a new one:  
            Molecule new_molecule=Reaction(molecules[0],molecules[1]);
            molecules.Add(new_molecule);

Here, one can print the content of one of the properties of the list for the 3 objects:在这里,可以打印 3 个对象的列表属性之一的内容:

            Console.WriteLine("Properties A and B of the 1st molecule:");
            Console.WriteLine(molecules[0].property_A);
            Console.WriteLine(molecules[0].property_B);
            Console.WriteLine("Property A and B of the 2nd molecule:");
            Console.WriteLine(molecules[1].property_A);
            Console.WriteLine(molecules[1].property_B);
            Console.WriteLine("Property A and B of the 3rd, new molecule:");
            Console.WriteLine(molecules[2].property_A);
            Console.WriteLine(molecules[2].property_B);
            Console.ReadLine();
        }
    }
}

The output: output:

Properties A and B of the 1st molecule:
10
20
Properties A and B of the 2nd molecule:
3
7
Properties A and B of the 3rd, new molecule:
13
13.5

So I need a csv file with the complete output:所以我需要一个带有完整 output 的 csv 文件:

10,20
3,7
13,13.5

I tried to find such method in the forums, but I only found examples for generic lists of arrays, and I was not able to make them work.我试图在论坛中找到这样的方法,但我只找到了 arrays 通用列表的示例,我无法让它们工作。 I really appreciate any help on this issue (I'm a beginner in C#).我非常感谢在这个问题上的任何帮助(我是 C# 的初学者)。

CSV files are pretty straight forward to generate: CSV 文件很容易生成:

using (StreamWriter writer = new StreamWriter("myfile.csv"))
{
    foreach (Molecule molecule in molecules)
    {
        writer.WriteLine($"{molecule.property_A},{molecule.property_B}");
    }
}

Have you tried System.IO.File yet?你试过System.IO.File了吗? It's a simple way of opening and writing to a file: example .这是一种打开和写入文件的简单方法: example .

It looks like you'll need some sort of method like the following;看起来您需要以下某种方法;

    WriteMoleculesToFile(string pathToFile, List<Molecule> molecules) {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@pathToFile)) {
            foreach (Molecule molecule in molecules) {
                file.WriteLine(String.Format("{0},{1}",molecule.property_A, molecule.property_B));
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM