0

I have an ASP.net project and want to return a CSV file when an AJAX post is sent (yes it works. See Handle file download from AJAX post). The special thing is, that I want to create the result in a MemoryStream to return it as FileResult. But my problem is now, that German umlauts (ä, ö, ü) get corrupted. So here is my code:

public ActionResult Download(FormCollection form) {
string[] v = new string[16];
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream, 
    System.Text.Encoding.GetEncoding("Windows-1252"));
SqlCommand cmd = dbconn.CreateCommand();
//create SQL command
while (rs.Read()) {
  v = new string[16];
  v[0] = rs.GetString("IstAktiv");
  v[1] = rs.GetString("Haus");
  //cache all the values
  ...
  //write cached values
  for (int i = 0; i < v.Length; i++) {
    if (i > 0) writer.Write(";");
    writer.Write(v[i]);
    writer.Flush();
  }
  writer.Write("\r\n");
  writer.Flush();
} //end while rs.Read()

FileContentResult ret = new FileContentResult(stream.ToArray(), "text/csv");
ret.FileDownloadName = "Kontakte.csv";
writer.Close();
return ret;
} //end method

So when I open the resulting file in Excel the umlauts are converted into something strange. For example the upper case letter "Ä" is changed to "�". So is there any possibility to solve this issue?

Best regards

Community
  • 1
  • 1
  • Have you tried with different encodings? – George Chondrompilas Jun 03 '15 at 11:52
  • `//System.Text.Encoding.GetEncoding("UTF-8") //*/ //System.Text.Encoding.GetEncoding(1141) //*/ //System.Text.Encoding.Unicode //System.Text.Encoding.GetEncoding("Windows-1250")` yes these were the other encodings, I tried. I got the best result with UTF-8. When opening the file in normal notepad everything was fine, but when I opened the same file in Excel the umlauts were corrupted anyway. So I save the file in notepad as ANSI-coded file and even Excel could display umlauts as they should – Mecki Matschbirne Jun 03 '15 at 11:55
  • Can you try with System.Text.Encoding.GetEncoding(850)? – George Chondrompilas Jun 03 '15 at 11:56
  • 1
    This isn't corruption, it's incorrect encoding. Instead of using Unicode, you are forcing a conversion to Windows 1252. Use Unicode instead – Panagiotis Kanavos Jun 03 '15 at 11:57
  • @GeorgeChond the problem is that the OP *is* trying to change encodings, resulting in conversion errors. .NET user Unicode throughout, there is no reason to try different encodings – Panagiotis Kanavos Jun 03 '15 at 11:57
  • Moreover, your code does't actually need the MemoryStream as you are sending raw bytes. Use StringWriter to write the data to a StringBuilder then use [Encoding.UTF8.GetBytes](https://msdn.microsoft.com/en-us/library/ds4kkd55%28v=vs.110%29.aspx) to get the bytes of the generated string – Panagiotis Kanavos Jun 03 '15 at 12:01
  • Hmm, Unicode doesn't work either. "Ä" in notepad is "뿯½" – Mecki Matschbirne Jun 03 '15 at 12:02
  • @PanagiotisKanavos I will try this – Mecki Matschbirne Jun 03 '15 at 12:03
  • Make sure you specify the correct encoding when returning the file. Also note that in Unicode, what you see as a single character is actually 2 bytes, with UTF8 it's 1-3. – Panagiotis Kanavos Jun 03 '15 at 12:04
  • @PanagiotisKanavos I tried a little with your given advice but the result failed. This is what I tried: `FileContentResult ret = new FileContentResult(System.Text.Encoding.UTF8.GetBytes("Änderung;Bla\r\nÜbel;bla2\r\n"), "text/csv"); ret.FileDownloadName = "Kontakte.csv"; Response.Charset = "utf-8"; return ret;` Any suggestions? – Mecki Matschbirne Jun 03 '15 at 12:47
  • What do you mean "failed"? If you try to open a Unicode file as ASCII in Notepad, characters above ASCII 127 will appear as 2-4 characters. Did you try to open it as a Unicode file? – Panagiotis Kanavos Jun 03 '15 at 12:49
  • On German systems I have good success with `iso-8859-1`. On a German system that's what `Encoding.Default` returns. Excel seems to use that one as well. – usr Jun 03 '15 at 13:11

1 Answers1

1

To have Excel read CSV files correctly, it expects the CSV file to be in the UTF-8 encoding (with BOM).

So, without a doubt, your StreamWriter would have to be set this way:

StreamWriter writer = new StreamWriter(stream, 
    System.Text.Encoding.GetEncoding("UTF-8"));

However, if that doesn't work for you, then it's very likely that it's because the characters are being corrupted before you even get a chance to write them to the stream. You may be facing an encoding conversion problem as you are reading the data from the database.

v = new string[16];
v[0] = rs.GetString("IstAktiv");
v[1] = rs.GetString("Haus");

To validate that, place a breakpoint as you read the values into the 'v' array, and check that the characters still look ok at this step. If they are corrupted, then you know that the problem is between the code and the database, and the writing to the CSV is not the problem.

EDIT: Here is an isolated test case you can use to prove that UTF-8 is the correct encoding to write CSVs. Perhaps you can try that first:

 Encoding enc = Encoding.GetEncoding("UTF-8");
 using (StreamWriter writer = new StreamWriter(@"d:\test\test.csv", false, enc))
 {
    writer.Write(@"""hello ä, ö, ü world""");
 }
sstan
  • 35,425
  • 6
  • 48
  • 66
  • I set the breakpoint and the data written into the values array `v` were correct. – Mecki Matschbirne Jun 03 '15 at 12:40
  • 1
    You don't need to create a new UTF8 encoding, it's available as a static property, [Encoding.UTF8](https://msdn.microsoft.com/en-us/library/system.text.encoding.utf8%28v=vs.110%29.aspx) – Panagiotis Kanavos Jun 03 '15 at 12:51
  • @Mecki Matschbirne: I recommend that you write yourself an isolated test case where you directly write the values to the stream, without reading from a database. That's what I just did, using the characters that you mention. And the only way I get them to open properly in Excel, is by making sure the CSV is in UTF-8 format (with BOM). Can you try that and let us know what results you get? – sstan Jun 03 '15 at 12:55
  • @Panagiotis Kanavos: correct. You can do that too. +1 – sstan Jun 03 '15 at 12:55
  • Are you sure Excel uses UTF-8? That has not been my experience. – usr Jun 03 '15 at 13:11
  • @usr: Well at the very least, it supports it. I am able to double-click the csv file and open it nicely in Excel. However, maybe different versions of Excel have different quirks? So I should mention that I am using Excel 2010 on Windows 8.1. IF OP still has problems, may he can tell us his version of Excel? – sstan Jun 03 '15 at 13:22