Export C # List to Csv File

I tried to export my C # list to a Csv file. Everything is set up well. But the fact is that the field separator is not working properly. its display, like, my line with "at the end (ex: 0000324df"). Here is my controller code.

IEnumerable stockexpo = stockexp; // Assign value MemoryStream output = new MemoryStream(); StreamWriter writer = new StreamWriter(output, Encoding.UTF8); writer.Write("ItemNo,"); writer.Write("Repeat Count"); writer.WriteLine(); foreach (StockResult order in stockexpo) { writer.Write(String.Format("{0:d}", order.ItemNumber)); writer.Write("\""); writer.Write(","); writer.Write("\""); writer.Write(order.Count); writer.Write("\""); writer.Write(","); writer.WriteLine(); } writer.Flush(); output.Position = 0; return File(output, "text/comma-separated-values", "stockexp.csv"); 

I need to know how I can appropriately separate field values. Anyone can help me with this.

0
source share
1 answer
  writer.Write("\""); 

This line of code will print "every time. Why is it at all?

Also, I would not have a comma before WriteLine, since there is no need to limit the end of the file.

 IEnumerable stockexpo = stockexp; // Assign value MemoryStream output = new MemoryStream(); StreamWriter writer = new StreamWriter(output, Encoding.UTF8); writer.Write("ItemNo,"); writer.Write("Repeat Count"); writer.WriteLine(); foreach (StockResult order in stockexpo) { writer.Write(order.ItemNumber); writer.Write(","); writer.Write(order.Count); writer.WriteLine(); } writer.Flush(); output.Position = 0; return File(output, "text/comma-separated-values", "stockexp.csv"); 
+3
source

Source: https://habr.com/ru/post/1482954/


All Articles