0

我正在尝试使用http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library#Installation上的 LinqToCSV 库从列表中写入 CSV 文件。

当我尝试运行我的程序时,我不断收到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 文件。任何帮助将不胜感激。

4

1 回答 1

1

您的代码似乎没有使用 Linq2Csv 的任何功能。

您收到错误是因为您只是想写出一个没有 csvcolumn 属性的字符串。

为什么你认为你需要 linq2csv?从您在 2 个文件中读取的代码中,对这些文件中的行进行排序,然后尝试将结果写回。您没有使用任何 Linq2csv 的功能。您可以直接将此结果写入文件而不使用 linq2csv。

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

更多信息

文章中的这一行读取数据

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

您需要为每个要读入 2 个列表的文件执行此操作,例如parts&parts2从您的代码中。

完成此操作后,请按照示例编写文件,但通过parts.union(parts2)而不是products2.

cc.Write(products2, "products2.csv", outputFileDescription);
于 2013-05-30T18:15:23.673 回答