csv export更改浮点字符串中的逗号位置

时间:2013-12-16 10:28:36

标签: c# asp.net csv

当我导出包含十进制数字的字符串值时,例如6,7014
导出到csv文件时,它转换为奇怪的值67,014
这是不合适的

我使用的

功能如下:

public void DownloadtoCSV(DataSet ds, String name)
{
    string deLimiter = ";";

    if (System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol == "$")
    {
        deLimiter = ",";
    }

    StringBuilder sb = new StringBuilder();
    sb.Append("sep=" + deLimiter + "\r\n");
    foreach (DataTable dataTable in ds.Tables)
    {
        if (dataTable.Columns.Count != 0)
        {
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {

                if (i == 0)
                {
                    sb.Append(dataTable.Rows[0][i].ToString().Replace(",", " ").Replace("\r\n", " "));
                }
                else
                {
                    sb.Append(deLimiter + dataTable.Rows[0][i].ToString().Replace(",", " ").Replace("\r\n", " "));

                }
            }
            sb.Append("\r\n");
            Int64 k = 0;
            if (dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    if (k != 0)
                    {
                        for (int i = 0; i < dataTable.Columns.Count; i++)
                        {
                            if (i == 0)
                            {

                                sb.Append("\"" + row[dataTable.Columns[i].ColumnName].ToString().Replace("\"", "\"\"").Replace("\r\n", " ") + "\"");

                            }
                            else
                            {

                                sb.Append(deLimiter + "\"" + row[dataTable.Columns[i].ColumnName].ToString().Replace("\"", "\"\"").Replace("\r\n", " ") + "\"");
                            }
                        }
                        sb.Append("\r\n");
                    }
                    k++;
                }
            }
        }
        sb.Append(Environment.NewLine);
    }
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    Response.ContentType = "Application/save";
    Response.AddHeader("content-disposition", "attachment;filename=" + name + ".csv");
    Response.ContentEncoding = Encoding.Default;
    Response.Write(sb.ToString());
    Response.End();
}`enter code here`

0 个答案:

没有答案