2

I get

 Quattrode® 

From a string that is HTML encoded as

Quattrode® 

when viewing inside Excel 2007.

Routine

        Response.Clear();
        Response.Buffer = true;

        Response.ContentType = "text/comma-separated-values";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"Expired.csv\"");

        Response.RedirectLocation = "export.csv";

        Response.Charset = "";
        //Response.Charset = "UTF-8";//this does not work either.
        EnableViewState = false;

        ExportUtility util = new ExportUtility();

        Response.Write(util.DataGridToCSV(gridViewExport, ","));

Basically DataGridToCSV() uses HttpUtility.HtmlDecode(stringBuilder.ToString()); and I can use visual studio's text visualizer and see the string looks correct (Quattrode®).

So something in the Response procedure or in Excel's interpretation of the file is not correct. Any idea how to fix?


UPDATE
Turns out that this is not interpreted properly in Excel or WordPad. I open it up in Notepad and the symbol shows up properly.
Suman Banerjee
  • 1,923
  • 4
  • 24
  • 40
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • I'm pretty sure you have to select the proper text encoding to use when importing CSV's into Excel, don't you? Make sure you're hitting the UTF-8 charset. – lsuarez Jun 22 '11 at 18:50

3 Answers3

8

I found the answer here https://stackoverflow.com/a/3300854/150342 and here https://stackoverflow.com/a/6460488/150342 Then put together this code:

    public static void WriteCSVToResponse(string csv, string filename)
    {
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearHeaders();
        response.ClearContent();
        response.ContentType = "text/csv";
        response.AddHeader("content-disposition", "attachment; filename=" + filename);
        response.ContentEncoding = Encoding.UTF8;

        byte[] BOM = new byte[] { 0xef, 0xbb, 0xbf };
        response.BinaryWrite(BOM);//write the BOM first
        response.BinaryWrite(Encoding.UTF8.GetBytes(csv));
        response.Flush();
        response.End();
    }
Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197
5

Also try this

Response.ContentEncoding = Encoding.UTF32;
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
Aristos
  • 66,005
  • 16
  • 114
  • 150
1

Response.Charset = "";

Try using something like UTF-8

BRampersad
  • 862
  • 1
  • 12
  • 25