This post will help you fix the issue where you csv export has corrupt characters when opening it in excel.

Here was the code before:

public HttpResponseMessage GetFileDownloadResponse(string fileName,string csvData)
{
    using (MemoryStream ms = new MemoryStream())
    {
        using (StreamWriter outfile = new StreamWriter(ms))
        {
            outfile.Write(csvData);
        }
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
        httpResponseMessage.Content = new ByteArrayContent(ms.ToArray());
        httpResponseMessage.Content.Headers.Add("x-filename", fileName);
        httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel");
        httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
        httpResponseMessage.StatusCode = HttpStatusCode.OK;
        return httpResponseMessage;
    }
}

We need to enable a BOM character that will tell Excel that the CSV file is in unicode

We do this by passing true when instaniating UTF8Encoding

here:

using (StreamWriter outfile = new StreamWriter(ms, new UTF8Encoding(true)))

and here:

httpResponseMessage.Content.Headers.ContentType.CharSet = new UTF8Encoding(true).HeaderName;

Here is the full code after:

public HttpResponseMessage GetFileDownloadResponse(string fileName,string csvData)
{

    using (MemoryStream ms = new MemoryStream())
    {
        using (StreamWriter outfile = new StreamWriter(ms, new UTF8Encoding(true)))
        {
            outfile.Write(csvData);
        }
        HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
        httpResponseMessage.Content = new ByteArrayContent(ms.ToArray());
        httpResponseMessage.Content.Headers.Add("x-filename", fileName);
        httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel");
        httpResponseMessage.Content.Headers.ContentType.CharSet = new UTF8Encoding(true).HeaderName;
        httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
        httpResponseMessage.StatusCode = HttpStatusCode.OK;
        return httpResponseMessage;
    }
}

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

Buy me a coffee

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to solve the error assets file project.assets.json not found in Visual Studio

This post tells you how to solve the error assets file project.assets.json not found in Visual Studi…

Read Post

Code to help you debug an umbraco issue on a remote site

This post gives you some razor code to help you see the values of the IPublishedContent item's prope…

Read Post

How to set the default page base type to UmbracoViewPage in Umbraco

In this post I share with you what Ronald Barendse taught us about setting the pageBaseType in Umbra…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post