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?

maliks
  • 1,102
  • 3
  • 18
  • 42
  • This is not the case, my data to be written is from variables in the code – maliks Jul 25 '16 at 11:18
  • Whereas I need to have Column headers at the top and values in csv format – maliks Jul 25 '16 at 11:18
  • 1
    Try to tell what you **can't** do. [Append line to file](http://stackoverflow.com/q/2837020/1997232)? Or maybe you don't know what is [csv-file](https://en.wikipedia.org/wiki/Comma-separated_values) ? Maybe the problem is [how to insert column headers](http://stackoverflow.com/q/32357209/1997232)? – Sinatr Jul 25 '16 at 11:19
  • I'm unable to show column headers once at the top of csv – maliks Jul 25 '16 at 11:21
  • That's [easy](http://stackoverflow.com/q/32357209/1997232). 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). – Sinatr Jul 25 '16 at 11:22
  • and me getting my data all in a single row instead different rows by using code which I've shown above – maliks Jul 25 '16 at 11:29

1 Answers1

-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.

Josiah Keller
  • 3,635
  • 3
  • 23
  • 35
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • There is some error i.e. `)` expected – maliks Jul 25 '16 at 11:42
  • 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 – maliks Jul 25 '16 at 11:44
  • Also the `$` sign unexpected – maliks Jul 25 '16 at 11:45
  • 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](https://msdn.microsoft.com/en-us/library/dn961160.aspx) (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). – Sinatr Jul 25 '16 at 11:47
  • See edit. C# 7.0 is *soon*... – Sinatr Jul 25 '16 at 11:53