Exporting a CSV file from ASP.NET with special characters


When you try to export a CSV file from ASP.NET that contains special characters like é å æ É à you may experience the following:

  • Opening the file in Notepad shows the correct characters
  • Opening the file in Excel, it shows characters like é Ã¥ æ É Ã

This also causes problems when you try to import this file in to CRM using the standard data imports.

To solve this issue you will have to use the correct encoding during the export of the file.

Imagine you have used a StringBuilder to generate the CSV file content.

Response.ContentType = “application/csv”;

Response.Charset = “”;

Response.AddHeader(“Content-Disposition”, “attachment;filename=queue-errors.csv”);

 

Response.ContentEncoding = Encoding.Unicode;

Response.BinaryWrite(Encoding.Unicode.GetPreamble());

 

Response.Write(sb.ToString());

Response.End();

Posted in .NET
2 comments on “Exporting a CSV file from ASP.NET with special characters
  1. Nadavd says:

    Thanks, Saved me time.

  2. Stefano Massone says:

    thanks!

Leave a comment