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?
StringBuilder) and add column headers line once outside (before) cycle. Then in the cycle you just add data into corresponding columns (typically using indexes).