CSV导出在货币字段上插入垃圾字符 [英] CSV Export inserting junk chars on currency fields

查看:195
本文介绍了CSV导出在货币字段上插入垃圾字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用以下方法将数据表导出为CSV:

  private  < span class =code-keyword> void  btnDownload_Click( object  sender,EventArgs e)
{
if (DB.ExportAllHouseDetails())
{
DataTable dt = DB.dsResults.Tables [ tbl_HouseDetails];

// ExportCSV(dt,@C:\ Users \ pmcma \ Documents\Visual Studio 2012 \Projects \Houses \Houses \bin\Debug \HouseList.csv); // + DateTime.Now);
ExportCSV(dt, @ C:\ Houses\HouseList.csv);

MessageBox.Show( 下载完成!);
}
else
{
MessageBox.Show( 未找到数据。);
}
}

public static void ExportCSV(DataTable dt, string fileName)
{
尝试
{
// 创建CSV文件网格数据将被导出。
StreamWriter sw = new StreamWriter(fileName, false );
// 首先我们将编写标题。
// DataTable dt = m_dsProducts.Tables [0];
int iColCount = dt.Columns.Count;
for int i = 0 ; i < iColCount; i ++)
{
sw.Write(dt.Columns [i]);
if (i < iColCount - 1
{
sw.Write( ) ;
}
}
sw.Write(sw.NewLine);

// 现在写下所有行。
foreach (DataRow dr in dt.Rows)
{
for int i = 0 ; i < iColCount; i ++)
{
if (!Convert.IsDBNull(dr [i]))
{
sw.Write(dr [i] .ToString());
}
if (i < iColCount - 1
{
sw.Write( );
}
}

sw.Write(sw.NewLine);
}
sw.Close();
}
catch (例外情况)
{
throw ex;
}
}





当我检查CSV文件时,应该是25000英镑的货币字段是输出为21000英镑,任何带有特殊字符的名字都不会按预期输出,例如Daithí输出为DaithÃ。有没有办法阻止这种情况发生并迫使CSV导出只是转储数据,因为它在SQL表中?

解决方案

在实例化StreamWriter时添加它问题:

 StreamWriter sw =  new  StreamWriter(fileName, false ,Encoding.GetEncoding(  UTF-8)); 


Hi I'm using the following method to export a datatable to CSV:

private void btnDownload_Click(object sender, EventArgs e)
{
    if (DB.ExportAllHouseDetails())
    {
        DataTable dt = DB.dsResults.Tables["tbl_HouseDetails"];

        //ExportCSV(dt, @"C:\Users\pmcma\Documents\Visual Studio 2012\Projects\Houses\Houses\bin\Debug\HouseList.csv"); // + DateTime.Now);
        ExportCSV(dt, @"C:\Houses\HouseList.csv");

        MessageBox.Show("Download Complete!");
    }
    else
    {
        MessageBox.Show("No data found.");
    }                    
}

public static void ExportCSV(DataTable dt, string fileName)
        {
            try
            {
                // Create the CSV file to which grid data will be exported.
                StreamWriter sw = new StreamWriter(fileName, false);
                // First we will write the headers.
                //DataTable dt = m_dsProducts.Tables[0];
                int iColCount = dt.Columns.Count;
                for (int i = 0; i < iColCount; i++)
                {
                    sw.Write(dt.Columns[i]);                    
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);

                // Now write all the rows.
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < iColCount; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }
                        if (i < iColCount - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }          



Thing is when I check the CSV file a currency field that should be £25000 is outputted as £21000 also any names with special chars are not outputted as expected for example Daithí is outputted as Daithí. Is there anyway to stop this from happening and force the CSV export to just dump the data as it is in the SQL Tables?

解决方案

Adding this when instantiating the StreamWriter solved the issue:

StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("UTF-8"));


这篇关于CSV导出在货币字段上插入垃圾字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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