Search code examples
.netunicodecsv

Unicode in CSV file?


I need to generate a CSV file. Maybe i am 'doing it wrong' because i am dumping the file with my own code instead of using a lib but anyways.

It looks like i have everything right. Quotes, commas and everything seems to be escaped perfectly. It was rather easy. The problem is i am using unicode strings to test and they come out as ????. When i use MS Excel to save a file with my test string and i hit save as CSV opening the file gets me the same problem (unicode letters becoming ?????). Is unicode not supported?

I just tried dumping the string like this instead of outputting it to a webpage

var f = new System.IO.StreamWriter(filename, false, System.Text.Encoding.Unicode);

and now i see the unicode text but everything is now in one column. Whats weird is everything looks normal in my text editor of choice and if i copy/paste a few columns out and paste it in saving as .csv i see the columns fine. Although it probably strips unicode out.

How do i save this properly?


Solution

  • System.Text.Encoding.Unicode uses UTF-16 encoding. Try telling your text-editors to decode with UTF-16; I'm guessing the editor you are using to display the output file is defaulting to UTF-8 or ASCII. If this is so, an alternative might be to encode the output with System.Text.Encoding.UTF8 instead.