简体   繁体   中英

Writing a .CSV file usingt the LinqtoCSV DLL

I'm trying to write to a CSV file from a list using the LinqToCSV library found on http://www.codeproject.com/Articles/25133/LINQ-to-CSV-library#Installation .

When I try and run my program I keep getting a CsvColumnAtrributeRequiredException exception. So it's not filling a CSV file.

Here is my code:

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);

I'm just trying to get this up and running to output it correctly to a CSV file. Any help would be greatly appreciated.

Your code doesn't appear to be using any of the features of Linq2Csv.

You get the error because you're just trying to write a string out which doesn't have csvcolumn attribute.

Why do you think you need linq2csv? From your code you read in 2 files sort the lines from those files then try to write the result back out. You're not using any of Linq2csv's features. You could just write this result straight to a file & not use linq2csv.

Alternatively, create a class that matches the data you are reading, then follow the instructions in the article to read the file into a list of that class. merge & write back out again using linq2csv.

More Info

This line from the article reads the data in

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

You need to do this for each file you want to read into 2 lists eg parts & parts2 from your code.

having done this, follow the example for writing a file, but pass parts.union(parts2) instead of products2 .

cc.Write(products2, "products2.csv", outputFileDescription);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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