0

我正在制作一个 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();

            }

        }

    } 
4

2 回答 2

2

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

有关更多详细信息,请参阅

字符编码和问题

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

于 2013-02-12T05:03:46.430 回答
0

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

于 2013-02-12T05:05:49.847 回答