1

I tried to write to CSV file using CsvHelper in C#. This is the link to the library http://joshclose.github.io/CsvHelper/ Nothing is sent to the csv file. I tried doing "exportCsv.WriteField("Hello");" but still nothing happened.

List<string> ColumnOne = new List<string>();
List<string> ColumnTwo = new List<string>();
var csvTextWriter = new 
    StreamWriter(@"C:\Users\Public\Documents\ExportTest.csv");
var exportCsv = new CsvWriter(csvTextWriter);
    //creating a list to store workflows then adding name and description to the myWorkflowsList list
   if (myWorkflows.WorkFlowCollection.Any())
   {
        foreach (var Workflow in myWorkflows.WorkFlowCollection)
        {
                    ColumnOne.Add(Workflow.WorkflowName);
                    ColumnTwo.Add(Workflow.WorkflowDescription);
        }
        exportCsv.WriteField(ColumnOne);
        //exportCsv.WriteField(ColumnTwo);
        exportCsv.NextRecord();
        exportCsv.Flush();
        Console.WriteLine("File is saved: 
                C:\\Users\\Public\\Documents\\ExportTest.csv");
        Console.ReadLine();
    }
6
  • I don't see any call that writes any records. No WriteRecords or WriteRecord. Only a weird call to WriteField that tries to add an an entire list of strings into a single cell Commented Mar 22, 2018 at 16:18
  • I want the output to be full of "workflow names" in column 1 and "workflow description" in collumn 2. so how do you recommend i use records? Commented Mar 22, 2018 at 16:21
  • Did you read CsvHelper's documentation site? It shows how to write records and how to write custom headers. Your code doesn't add any records Commented Mar 22, 2018 at 16:22
  • I did. but adding records in my case would not work Commented Mar 22, 2018 at 16:23
  • It works. Not adding records though is guaranteed to not write results. Commented Mar 22, 2018 at 16:24

1 Answer 1

4

Your code doesn't add any records. It doesn't have any calls to WriteRecords or WriteRecord. It looks like it's trying to write an entire list of strings into a single field instead.

To write two columns out to a file you can use `WriteRecords, eg :

var data = from flow in myWorkflows.WorkFlowCollection
           select new { flow.WorkflowName,flow.WorkflowDescription};
using (var writer = new StreamWriter("test.csv"))
using (var csv = new CsvWriter(writer))
{
    csv.WriteRecords(data);
}

This will write a file with field names WorkflowName and WorkflowDescription

You can change how the fields are written by creating a small class that accepts only the fields you want and sets names etc through attributes :

class Flow
{

    [NameAttribute("Workflow Name")]
    public string WorkflowName { get; set; }
    [NameAttribute("Workflow Description")]
    public string WorkflowDescription { get; set; }

    public Flow(string workflowName, string workflowDescription)
    {
        WorkflowName = workflowName;
        WorkflowDescription = workflowDescription;
    }
}


//...
var data = from flow in myWorkflows.WorkFlowCollection
           select new Flow(flow.WorkflowName,flow.WorkflowDescription);
using (var writer = new StreamWriter("test.csv"))
using (var csv = new CsvWriter(writer))
{
    csv.WriteRecords(data);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.