文本正在转换为符号

时间:2013-02-12 05:00:32

标签: c# visual-studio-2010

我正在制作一个adz收藏家,所以我从一个网站获得广告,然后我拿取html获取标题,价格,描述。并在最后输入DataTable将DataTable导出为CSV。但问题是文本在代码中很好,但是当它导出到CSV时就像:

 · 75% of the Controller’s time will focus on accounting: Their role includes:  o 
 Bookkeeping  o Payroll  o Monthly HST  o Trust accounting; Ensuring compliance with the     Real 
 Estate Council requirements  o Financial Statement Preparation  · 25% Will be       management 
 functions:  o Supervise and assist with conveyancing  o Supervise all the office staff (4 - 
 6)  o Other day to day management functions.   Requirements and Qualifications  Essential 
 Skills   · Experience working with government regulated financial reporting  · Experience 
 working with large numbers of people in a customer service oriented role  ·     Experience with 
 Trust Accounting    Additional Assets ....

到处都有符号,我用来导出的代码如下:

public  void DataTable2CSV(DataTable table, string filename, string seperateChar)
    {

        StreamWriter sr = null;

        try
        {

            sr = new StreamWriter(filename, true);
            string seperator = "";
            StringBuilder builder = new StringBuilder();


                foreach (DataColumn col in table.Columns)
                {

                    builder.Append(seperator).Append(col.ColumnName);

                    seperator = seperateChar;
                }

                sr.WriteLine(builder.ToString());


            foreach (DataRow row in table.Rows)
            {

                seperator = "";
                builder = new StringBuilder();
                foreach (DataColumn col in table.Columns)
                {

                    builder.Append(seperator).Append(row[col.ColumnName]);
                    seperator = seperateChar;

                }

                sr.WriteLine(builder.ToString());

            }

        }

        finally
        {

            if (sr != null)
            {

                sr.Close();

            }

        }

    } 

2 个答案:

答案 0 :(得分:2)

您有文字编码混淆。换句话说,您写入CSV文件的数据编码与CSV查看器(例如Excel)所需的编码不匹配。

有关详细信息,请参阅

Character Encoding and the ’ Issue

  

在特定的示例中,这是使用UTF-8读取的Unicode字符'RIGHT SINQLE QUOTATION MARK'(U + 2019)'的典型CP1252表示。在UTF-8中,该字符存在字节0xE2,0x80和0x99。如果您检查CP1252代码页布局,那么您将看到这些字节恰好代表字符â€,和。™。

答案 1 :(得分:0)

最可能的原因可能是您的系统和CSV无法支持的字体。查看本文以获取编码帮助。 http://office.microsoft.com/en-us/help/choose-text-encoding-when-you-open-and-save-files-HA010121249.aspx