TheSwamp

Code Red => .NET => Topic started by: Jeff_M on March 11, 2020, 10:16:09 AM

Title: Degree symbol for angles troubles
Post by: Jeff_M on March 11, 2020, 10:16:09 AM
I have a form with a DataGridView which has a column for angles. I format the angle to display how the user's angle units are set which works just fine. I also allow the user to output the data to a file or a printer. The output to printer looks exactly like it does in the form, the output to a file does as well: ,0.000,S 61°10' 24" W,3.580, That is a portion of a line opened in Notepad. However, when the same file is opened in Excel, the angle column has an extra character inserted: S 61°10' 24" W

Now, if I remove that character in Excel, save, close, reopen, it looks just fine and looks the same in Notepad as it did before. So why is Excel treating the initial file differently than the one it saved? In my form's tools to convert the angle from Radians to Degrees it first creates a string like so: DDdMM'SS" then replaces the d with the degree symbol using this:  value = Regex.Replace(value, @"d", @"°");

Any suggestions on what I must do to get Excel to not add that  to the string?
Title: Re: Degree symbol for angles troubles
Post by: MarioR on March 11, 2020, 10:24:08 AM
Hello JeffM,

the "°" is ANSI display for the UTF8 "°".  Check in Notepad++ the coding, it should UTF8. What is your Excel-version? My Excel 2019 reads utf8-file correctly.

regards Mario
Title: Re: Degree symbol for angles troubles
Post by: huiz on March 11, 2020, 10:27:38 AM
If you write CSV you can apply Text.Encoding.Default:


https://stackoverflow.com/questions/8121005/what-is-the-default-encoding-when-saving-a-text-file (https://stackoverflow.com/questions/8121005/what-is-the-default-encoding-when-saving-a-text-file)
Title: Re: Degree symbol for angles troubles
Post by: Jeff_M on March 11, 2020, 10:56:27 AM
Thanks guys! I'm using Office 365 and Excel was just updated 4 days ago. The client who brought this up is also using Excel 365.
Title: Re: Degree symbol for angles troubles
Post by: Jeff_M on March 11, 2020, 12:50:32 PM
I was able to correct the output by changing 1 line of code. From this:
Code - C#: [Select]
  1.       using (StreamWriter sw = File.AppendText(filename))
  2.  
to this:
Code - C#: [Select]
  1.       using (StreamWriter sw = new StreamWriter(filename, true, Encoding.UTF8))
  2.  
Title: Re: Degree symbol for angles troubles
Post by: nobody on March 15, 2020, 12:23:11 AM
It's pretty scary when you have to post a question Jeff...I'm convinced very few can solve it if you can't lol ... luckily they are at the swamp :)