'StreamWriter writes all data into 1 Column in CSV file
using (StreamWriter sw = File.CreateText("D:\\Data.csv"))
{
foreach (var test in data)
{
sw.WriteLine(test.A.ToString()+','+test.B.ToString());
}
}
test.A and test.B are listed in column A of CSV file.I want them to be in the separate columns.
Solution 1:[1]
Try changing your delimiter to semicolon - it will separate your headers into different columns when you open the CSV file via Excel.
Solution 2:[2]
I had the same issue: I was able to write to a .txt or .csv with commas as the separators, but in Excel and other apps (python's Pandas) it wasn't being recognized as separate columns properly, my data was all in one column.
I solved this by modifying the Encoding to utf-8: https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=net-6.0
This is the default for SW but you might have mistakenly changed this elsewhere.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user18717719 |
