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;
    }
}

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.

Proudly sponsored by

AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
stkrs

  • Custom stickers for startups, artists and brands.
  • Bespoke easy-apply, removable, custom brand stickers printed in the UK.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.