cancel
Showing results for 
Search instead for 
Did you mean: 

Saving csv file with ANSI encoding

NupurSood
Level 5
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.
File.WriteAllText(File_Name, Text, System.Text.Encoding.GetEncoding(Encoding_Type))
Any guidance would be great help.

Thank YOu 
7 REPLIES 7

ewilson
Staff
Staff
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?

File.WriteAllText(File_Name, Text System.Text.Encoding.ASCII)
​

Cheers,
Eric

ewilson
Staff
Staff
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

NupurSood
Level 5
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
36913.png

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


KirtiMaan Talwar
IA Consultant
Deloitte USI

ewilson
Staff
Staff
@NupurSood,

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

NupurSood
Level 5
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

NupurSood
Level 5
Apologies, I had a typo.

That worked like a charm. Thank you so much for your help 🙂