使用LinqtoCSV DLL编写.csv文件

本文关键字:csv 文件 编写 DLL LinqtoCSV 使用 | 更新日期: 2023-09-27 18:16:13

我正在尝试使用在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文件。任何帮助都将非常感激。

使用LinqtoCSV DLL编写.csv文件

您的代码似乎没有使用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);