Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pound sign displaying as £ when generating CSV

I have a script which loops through a GridView on my web page, and generates a CSV file out of the values.

An issue was reported with this where symbols were being displayed encoded such as:

Lorem & Ipsum
£100

I've used Server.HtmlDecode(), and this has fixed the decoded the values, fixing the ampersand, however the pound sign is now displaying a different symbol:

Lorem & Ipsum
£100

Why is this, and how can I fix this so that the  character doesn't appear?


The code I'm using to prep 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 the HTML, replaces any commas and line breaks.

like image 303
Curtis Avatar asked Feb 22 '12 10:02

Curtis


People also ask

What is â in csv file?

When opening the CSV file in Excel, the right single quotation mark character is converted into the "’" symbol. This is because Excel opens the CSV file using the default windows encoding (Windows-1252) and hence the character is encoded as per the Windows-1252 characterset.

What is pound sign in Python?

In Python, any line of instructions containing the # symbol ("pound sign" or "hash") denotes the start of a comment. The rest of the line will be ignored when the program is run.


1 Answers

It's likely that your source string encoding doesn't match the output encoding. Set the content encoding before you write out the client to avoid a change in encoding that produces the results you've observed.

You can do this by setting Response.ContentEncoding; for example:

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

Will give 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 set this consistently across your application by using the globalization element in your web.config.

like image 156
dash Avatar answered Oct 13 '22 03:10

dash