Saving csv file with ANSI encoding
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
13-10-22 09:57 PM
Hello All
We have a usecase where we need to store a csv string in a text file. To achieve the same we are leveraging "Write text file" action in "Utility - file management". However, the csv file created has UTF-8 encoding but we desire "ANSI" encoding. Would you please be able to suggest a method for the same. We tried writing a code where we are able to define encoding type but it doesn't take "ANSI" to be a valid encoding type.
We have a usecase where we need to store a csv string in a text file. To achieve the same we are leveraging "Write text file" action in "Utility - file management". However, the csv file created has UTF-8 encoding but we desire "ANSI" encoding. Would you please be able to suggest a method for the same. We tried writing a code where we are able to define encoding type but it doesn't take "ANSI" to be a valid encoding type.
File.WriteAllText(File_Name, Text, System.Text.Encoding.GetEncoding(Encoding_Type))
Any guidance would be great help.
Thank YOu
Thank YOu
7 REPLIES 7
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-10-22 03:27 AM
Hello @NupurSood,
GetEncoding() returns an Encoding object based on a input code page. If you know you want ASCII why not just pass Encoding.ASCII in the WriteAllText() method?
Cheers,
Eric
GetEncoding() returns an Encoding object based on a input code page. If you know you want ASCII why not just pass Encoding.ASCII in the WriteAllText() method?
File.WriteAllText(File_Name, Text System.Text.Encoding.ASCII)
Cheers,
Eric
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-10-22 03:48 AM
I just realized you were asking about ANSI as opposed to ASCII. 🤦♂️ Apologies for my lack of reading comprehension.
What code page are you passing into GetEncoding()? Is it iso-8859-1?
Cheers,
Eric
What code page are you passing into GetEncoding()? Is it iso-8859-1?
Cheers,
Eric
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-10-22 05:38 PM
Hello @ewilson
Thank you for your prompt response. I just need to be able to save a csv text as ANSI coded which can be done manually as shown in attached picture but we were hoping for a code for the same. I might be using the wrong command here but if i pass "Encoding_Type" as "UTF-8" the file gets saved as UTF-8. However if I pass "Encoding_Type" value as "ANSI", I get an error saying it is not a valid encoding
Thank you for your prompt response. I just need to be able to save a csv text as ANSI coded which can be done manually as shown in attached picture but we were hoping for a code for the same. I might be using the wrong command here but if i pass "Encoding_Type" as "UTF-8" the file gets saved as UTF-8. However if I pass "Encoding_Type" value as "ANSI", I get an error saying it is not a valid encoding
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-10-22 05:53 PM
Hi @NupurSood,
Can you try by searching the required encoding from the list and using the .Net Name for the same as Encoding_Type.
https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers?redirectedfrom=MSDN
I hope this helps!!
Many Thanks,
KirtiMaan Talwar
Can you try by searching the required encoding from the list and using the .Net Name for the same as Encoding_Type.
https://learn.microsoft.com/en-us/windows/win32/intl/code-page-identifiers?redirectedfrom=MSDN
I hope this helps!!
Many Thanks,
KirtiMaan Talwar
KirtiMaan Talwar
IA Consultant
Deloitte USI
IA Consultant
Deloitte USI
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
14-10-22 09:22 PM
@NupurSood,
This works for me:
Cheers,
Eric
This works for me:
Dim data As String = "1,2,3,4,5,6,7,8,9,0"
System.IO.File.WriteAllText("C:\temp\TestFile.csv", data, System.Text.Encoding.GetEncoding("iso-8859-1"))
Cheers,
Eric
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
19-10-22 08:25 PM
Hello @ewilson
I tried this but this still saves my file as UTF-8 encoding and not ANSI. Might I be missing some pre-condition here?
Regards
Nupur
I tried this but this still saves my file as UTF-8 encoding and not ANSI. Might I be missing some pre-condition here?
Regards
Nupur
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
19-10-22 08:54 PM
Apologies, I had a typo.
That worked like a charm. Thank you so much for your help 🙂
That worked like a charm. Thank you so much for your help 🙂