0

I have my insertion into text file code as:

foreach (var kvauthor in _tauthorData)
{
   foreach (var coAuthor in kvauthor.Value.CoAuthors)
   {
      twObjClus.WriteLine("AuthorID: {0}, AuthorName: {1}, ClusterID: {2}, PaperID: {3},  
                           CoAuthors: {4}, PaperCategory: {5}, Venue: {6}, Year: {4}",  
                           eAuthor.AuthorID, eAuthor.AuthorName, curCluster.GetClusterID(),  
                           kvauthor.Key, coAuthor, kvauthor.Value.PaperCategory,  
                           kvauthor.Value.VenueID, kvauthor.Value.Year);

   }
}  

I want to insert all these data into a csv file whereas I've tried this as:

var csv = new StringBuilder();  

foreach (var kvauthor in _tauthorData)
{
   foreach (var coAuthor in kvauthor.Value.CoAuthors)
   {
      csv.AppendFormat("{0},{1},{2},{3},{4},{5},{6}",AuthorID: {0}, AuthorName: {1}, ClusterID: {2}, PaperID: {3},  
                           CoAuthors: {4}, PaperCategory: {5}, Venue: {6}, Year: {4}",  
                           eAuthor.AuthorID, eAuthor.AuthorName, curCluster.GetClusterID(),  
                           kvauthor.Key, coAuthor, kvauthor.Value.PaperCategory,  
                           kvauthor.Value.VenueID, kvauthor.Value.Year);

   }
}  

How can I insert this data row by row into csv file with column headers?

6
  • This is not the case, my data to be written is from variables in the code Commented Jul 25, 2016 at 11:18
  • Whereas I need to have Column headers at the top and values in csv format Commented Jul 25, 2016 at 11:18
  • 1
    Try to tell what you can't do. Append line to file? Or maybe you don't know what is csv-file ? Maybe the problem is how to insert column headers? Commented Jul 25, 2016 at 11:19
  • I'm unable to show column headers once at the top of csv Commented Jul 25, 2016 at 11:21
  • That's easy. You have to create file (or StringBuilder) and add column headers line once outside (before) cycle. Then in the cycle you just add data into corresponding columns (typically using indexes). Commented Jul 25, 2016 at 11:22

1 Answer 1

-1

Adding column headers is trivial, just do it once (when opening file or creating instance of e.g. StringBuilder which will be used to add rows later).

var csv = new StringBuilder();  

// add column headers first
csv.AppendLine("AuthorID,AuthorName, ..."); // and so on

// add data
foreach (var kvauthor in _tauthorData)
    foreach (var coAuthor in kvauthor.Value.CoAuthors)
    {
        csv.AppendLine($"{coAuthor.AuthorId},{coAuthor.Name}, ..."); // and so on
        // if you don't have C# 6.0
        // csv.AppendFormat("{0},{1}, ... {N}", coAuthor.AuthorId, coAuthor.Name, ..., Environmental.NewLine");
    }

File.WriteAllText(path, csv.ToString());

Note: you can use AppendLine to add Environmental.NewLine automatically, otherwise (if using AppendFormat) add it to the end of string manually.

Sign up to request clarification or add additional context in comments.

5 Comments

There is some error i.e. ) expected
I've used it as csv.AppendLine($"{eAuthor.AuthorID},{eAuthor.AuthorName},{curCluster.GetClusterID()},{kvauthor.Key},{coAuthor},{kvauthor.Value.PaperCategory},{kvauthor.Value.VenueID},{kvauthor.Value.Year}"); but getting error i.e. ) expected
Also the $ sign unexpected
If you seek debug help, then post your code and exact error message (as a new question of course). I am not able to help with information given in that comment line. If you don't have interpolated strings (require C# 6.0), then you have to use string.Format() or keep as you do now AppendLine, but simply add Environmental.NewLine at the end (as per note in the answer).
See edit. C# 7.0 is soon...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.