从包含unicode的数据库将数据导出到csv文件 [英] Export data to csv file from database which contains unicode

查看:126
本文介绍了从包含unicode的数据库将数据导出到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据导出到csv文件时出现问题:

There is some problem while I want to export data to csv file:

public void ExportLoalDataToCSV(DataTable dt, string fileName)
{
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
    IEnumerable<string> query = (from row in dt.AsEnumerable()
                                 select row.Field<string>("ID") + "," + row.Field<string>("Text")).Distinct();
    foreach (string row in query)
    {
        string[] temp = row.Split(',');
        foreach (string item in temp)
        {
            sw.Write(item + ",");
        }
    }
    sw.Close();
}



如果 row.Field< string>(Text)包含',',还会有另一个额外的列,这不是我的预期。 :(

(从代码中,我只想得到2列,ID和文字)

有没有办法解决这个问题?



非常感谢。

Jessie


while if row.Field<string>("Text") contains a ',', there will be another extra column which is not my expected. :(
(From the code, I just want to get 2 columns, ID and Text)
Is there any way to reslove this problem?

Thanks a lot.
Jessie

推荐答案

要么为逗号提出转义序列,例如&逗号然后在拆分后将其替换为。



或将每个字段放在''引号中并解析字符串以忽略引号之间的逗号



或使用更不可能的字符来分隔你的字段,例如^而不是逗号
Either come up with an escape sequence for comma e.g. &comma& then replace this with , after the split.

or put each field in '' quotes and parse the string to ignore commas in between quotes

or use a more unlikely character to separate your fields e.g. ^ instead of comma


我发现类似问题的解决方案是插入BOM字符到.CSV文件以指示其编码。

CSV和BOM字符 - CSV [ ^ ]

如果您甚至插入一个记事本文件的UNICODE字符,并试图保存它,你会看到一个警告文件的内容将丢失,它与.CSV文件相同,一般是纯文本文件。默认编码为ASCII,您需要将其更改为UTF-8。
The solution I have found to similar problems was to insert a BOM character to the .CSV file to indicate its encoding.
CSV and BOM character - CSV[^]
If you even inserted a UNICODE character to a Notepad file, and tried to save it, you would have seen a warning that the contents of the file will be lost, it is the same with .CSV file which are plain text files in general. The default encoding is ASCII and you need to change it to UTF-8.


您可以尝试将字符串包装在引号中:



You could try wrapping your strings in quotations:

IEnumerable<string> query = 
(
     from row in dt.AsEnumerable()
     select row.Field<string>("ID") 
          + ", \""
          + row.Field<string>("Text") 
          + "\""
).Distinct();





另外,由于您将逗号作为选择查询的一部分包含在内,我认为在写出之前您不需要拆分字符串。



Also, since you are including the comma as a part of your selection query, I don't think you will need to split the string before writing it out.


这篇关于从包含unicode的数据库将数据导出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆