无法在不丢失阿拉伯值的情况下将 wpf 中的数据网格转换为 csv

Cannot convert data-grid in wpf to csv without missing Arabic values

我正在开发一个从 JSON API 获取数据并将其显示在数据网格中的项目,用户可以根据需要修改其中的值,然后导出为 CSV,数据包含阿拉伯文本,导出 CSV 后变成问号而不是实际的阿拉伯文本!!!

这里有一个代码供参考:

        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);

        }

我已经尝试使用不同的编码,如 ASCII 和 Unicode,但没有显示所需的结果,即 CSV 中的阿拉伯文本,没有问号 谢谢

检查 this 如何从剪贴板获取 Unicode。

我已经设法找到解决这个问题的方法

首先转换为 Unicode 文本,然后将“\t”替换为“,” 之后,将其保存为 CSV,代码如下:

        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);

        }