Pound sign displayed as  when generating CSV

I have a script that goes through a GridView on my webpage and generates a CSV file from the values.

An error was reported when characters were displayed in encoding, for example:

Lorem & Ipsum £100 

I used Server.HtmlDecode() and this fixed the decoded values, correcting the ampersand, however the pound sign now displays a different character:

 Lorem & Ipsum £100 

Why is this and how can I fix it so that the  symbol does not appear?


The code that I use to prepare the initial value for use in CSV is:

 Dim Str As String = String.Format("{0}", Server.HtmlDecode(value).Replace(",", "").Replace(Environment.NewLine, " ").Replace(Chr(10), " ").Replace(Chr(13), " ")) 

This decodes HTML, replaces any commas and line breaks.

+4
source share
1 answer

Your source string encoding probably doesn't match the output encoding. Define the encoding of the content before you write out the client to avoid a change in the encoding that produces the results that you observed.

You can do this by setting Response.ContentEncoding; eg:

 Response.ContentEncoding = System.Text.Encoding.GetEncoding(1252); 

Gives you the Windows 1252 code page. You can also use UTF-8 and ISO 8559-1 or, indeed, any other encoding you want.

You can also install this sequentially in your application using the globalization element in your web.config.

+10
source

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


All Articles