ASP.NET CSV Excel problem with weird characters

I export the data table to CSV format, for example:

    "COL1","COL2","COL3"
    "1","some text", "£232.00"
    "2","some more text", "£111.00"
    "3","other text", "£2.00"

The export code is pretty simple using the ashx handler:

    context.Response.Clear()
    context.Response.ContentType = "text/csv"
    context.Response.AddHeader("Content-disposition", "attachment;filename=data.csv")
    context.Response.AddHeader("Cache-Control", "must-revalidate")
    context.Response.AddHeader("Pragma", "must-revalidate")
    context.Response.Write(data)
    context.Response.Flush()
    context.Response.End()

My problem is when Excel tries to open the exported file, the character Âappears in front of all the characters £, for example. £232.00when the value should be £232.00.

+3
source share
2 answers

You need to set the encoding, i.e. property Response.ContentEncoding.

Reflector ContentEncoding, , Encoding.Default Encoding.UTF8, system.web/globalization, responseEncoding .

, ContentEncoding :

  • , , ;
  • , , HttpWriter.UpdateResponseEncoding, HttpWriter.FlushCharBuffer , , - .
  • ContentEncoding, , , HttpWriter.UpdateResponseEncoding, , - , HttpWriter._responseEncodingUpdated True HttpWriter.UpdateResponseEncoding.

, , OP Unicode, , , system.web/globalization, .

:

  • , Response.Clear, , , , , , , ContentEncoding .
  • Reflector 6 System.Web .NET Framework 2.0.
+5

-

Response.ContentEncoding = System.Text.Encoding.Default
0

Source: https://habr.com/ru/post/1737035/


All Articles