The following code outputs the result to a csv file. Lets say there are 10 values in the ‘valueDataList’ … When this is run, only one row is outputted to the file. How can I loop through the code to print the file for all values ‘at once’ in “valueDataList”.
foreach (var value in valueDataList)
{
var value1 = xxxxxxx // this value is set here based on a query..
int value2 = xxxxxxxx // this value is set here based on a query..
byte[] content;
using (var ms = new MemoryStream())
{
using (var writer = new StreamWriter(ms))
{
writer.WriteLine("ValueHeading1 " + " ValueHeading2");
writer.WriteLine(value1 + " " + value2);
}
content = ms.ToArray();
return File(content, "text/csv", "demo.csv");
}
}
Basically I want to be able to append to an existing csv file on each loop iteration. How can I do this? thanks for your help.
Do you mean you want to write all rows from valueDataList to a csv-file?
If so;