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();

Advertisements
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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: