Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1200

BOM Merging Excel columns while working on CSV C#

$
0
0

I am working on a CSV file where a user will download a template which contains header field and after that user will enter the data into the csv and save the csv after that upload the csv into the system.

it was working fine until one of our client informed us to add Arabic support into the csv.

our historical code looked like below

byte[] fileBytes = Encoding.ASCII.GetBytes("prefilled data, Header Data 1, Header Data 2\n abc");//not an actual code as I changed this code few weeks backreturn File(fileBytes, "text/csv", Path.GetFileName(data.FilePath));

after the request we have updated our to something like below

byte[] csvBytes = new UTF8Encoding(true).GetBytes("prefilled data, Header Data 1, Header Data 2\n رخصةتجارية");return File(csvBytes, "text/tsv", fileName.Replace(".csv", ".tsv"));

but even this was not working Screenshot for your reference. Screenshot

after some google search and a help from chatGPT I come to know that Excel is not recognizing the file as utf-8 and I should use BOM to help excel to recognize the file as UTF-8.

after that I did the below code with the help of chatGPT.

byte[] utf8Bom = new UTF8Encoding(true).GetPreamble(); // `true` to add BOMbyte[] csvBytes = new UTF8Encoding().GetBytes("prefilled data, Header Data 1, Header Data 2\n رخصةتجارية");byte[] resultBytes = new byte[utf8Bom.Length + csvBytes.Length];Buffer.BlockCopy(utf8Bom, 0, resultBytes, 0, utf8Bom.Length);Buffer.BlockCopy(csvBytes, 0, resultBytes, utf8Bom.Length, csvBytes.Length);return File(resultBytes, "text/csv", fileName);

after this the excel is now able to handle arabic text but the problem is when I fill all the data and save it and open the file again it is merging all the column into one and I open the data again in notepad++ the "," is replaced automatically with " " (tab).

how to fix this.


Viewing all articles
Browse latest Browse all 1200

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>