[c#]写作。CSV 文件 usingt LINQtoCSV dll

标签: csv dll C#
发布时间: 2013/12/8 0:07:24
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想从列表中使用 LINQtoCSV 库上,发现写入 CSV

http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library#Installation

每当我试着运行我的程序就会不断给"CsvColumnAtrributeRequiredException"的异常。所以它不灌装的 CSV 文件。这里是我的代码,

    private void button1_Click(object sender, EventArgs e) // merge files button
    {
        if(System.IO.File.Exists("OUTPUT.csv"))
        {
            System.IO.File.Delete("OUTPUT.csv");
            output = new System.IO.StreamWriter("OUTPUT.csv");
        }

        else
        {
            output = new System.IO.StreamWriter("OUTPUT.csv");
        }


        String[] parts = new String[1000];
        String[] parts2 = new String[1000];

        parts = File.ReadAllLines(textBox1.Text);       //gets filepath from top textbox
        parts2 = File.ReadAllLines(textBox2.Text);      //gets filepath from middle textbox




        String[] head = File.ReadAllLines(headingFileBox.Text); //header file array


        //merging the two files onto one list, there is no need to merge the header file because no math is being
        //computed on it
        var list = new List<String>();
        list.AddRange(parts);
        list.AddRange(parts2);



        //foreach loop to write the header file into the output file
        foreach (string h in head)
        {
            output.WriteLine(h);
        }

        //prints 3 blank lines for spaces
        output.WriteLine();
        output.WriteLine();
        output.WriteLine("LEASE NAME" + "," + "FIELD NAME" + "," + "RESERVOIR" + "," + "OPERATOR" + "," +"COUNTY" + "," + "ST" + "," + "MAJO" + "," + "RESV CAT" + "," + "DISCOUNT RATE"
            + "," + "NET OIL INTEREST" + "," + "NET GAS INTEREST" + "," + " WORKING INTEREST" + "," + "GROSS WELLS" + "," + "ULT OIL" + "," + "ULT GAS" + "," + "GROSS OIL" + "," 
            + "GROSS NGL" + "," + "GROSS GAS" + "," + "NET OIL" + "," + "NET GAS" + "," + "NET NGL" + "," +"REVENUE TO INT." + "," + "OPER. EXPENSE" + "," + "TOT INVEST." + "," 
            + "REVENUE OIL" + "," + "REVNUE GAS" + "," + "OPERATING PROFIT" + "," + "REVENUE NGL" + "," + "DISC NET INCOME" + "," + "SEQ" + "," + "WELL ID" + "," + "INC ASN" + "," 
            + "LIFE YEARS" + "," + "OWN QUAL" + "," + "PRODUCTION TAX" + "," + "AD VALOREM TAX");
        output.WriteLine();
        output.WriteLine();

        String[] partsComb = list.ToArray(); // string array that takes in the list
        Array.Sort(partsComb);

        CsvFileDescription outputFileDescription = new CsvFileDescription
        {
            SeparatorChar = '\t',
            FirstLineHasColumnNames = false, 
        };

        CsvContext cc = new CsvContext();
        cc.Write(partsComb ,output, outputFileDescription);

我只是想要先把这个和运行正确的输出到一个 CSV 文件。任何帮助非常感谢。

解决方法 1:

您的代码不会出现将使用的任何 Linq2Csv 的功能。

您遇到的错误,因为你只想写出其中的字符串并不具有 csvcolumn 属性。

你为什么觉得你需要 linq2csv?从您的代码你的那些文件中读取的 2 个文件排序行然后尝试写入结果出来。您不使用任何 Linq2csv 的功能。你可能只是直接向文件中写入这一结果 & 不使用 linq2csv。

另外,创建一个与您正在阅读,然后按照文章中的说明文件读入的那类列表的数据匹配的类。合并和回写出再次使用 linq2csv。

更多的信息

这条线从文章中读取数据

IEnumerable<Product> products = cc.Read<Product>("products.csv", inputFileDescription);

您需要为您想要 eg 读入 2 列出每个文件做这 parts & parts2 从您的代码。

做完这些,从示例中写入一个文件中,但通过 parts.union(parts2) 而不是 products2

cc.Write(products2, "products2.csv", outputFileDescription);
赞助商