I am working on a project which gets data from a JSON API and displays it in a data-grid in-which the user can modify the values if required then export as CSV,the data contains Arabic text which after exporting CSV becomes a question marks instead of the actual Arabic text!!!
here is a code for reference:
private void ExportToCSV(DataGrid dg)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
dg.UnselectAllCells();
String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
//Save Location for the csv (not the actual Location)
string SaveLocation = @"C:\Users\username\...\values" + ".csv";
//Overwriting previous values after exporting
File.Delete(SaveLocation);
File.AppendAllText(SaveLocation, result,Encoding.UTF8);
}
I have tried using different Encoding like ASCII and Unicode but don't show the required result which is Arabic text inside CSV without question marks Thank You
I have managed to find a solution to this problem
by first converting to Unicode text then replacing '\t' with ',' afterward, save it as CSV, here is the code:
private void ExportToCSV(DataGrid dg)
{
dg.SelectAllCells();
dg.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
ApplicationCommands.Copy.Execute(null, dg);
dg.UnselectAllCells();
String result =(string)Clipboard.GetData(DataFormats.UnicodeText);
string resultCSV = result.Replace('\t',',');
//Save Location for the csv (not the actual Location)
string SaveLocation = @"C:\Users\username\...\values" + ".csv";
//Overwriting previous values after exporting
File.Delete(SaveLocation);
File.AppendAllText(SaveLocation, result,Encoding.UTF8);
}