为什么File.WriteAllText()通过添加逗号来格式化我的数字?

时间:2013-11-12 19:41:18

标签: c# csv file-io

我不明白为什么会这样,但确实如此。我正在写一个像这样的csv文件:

foreach (var row in record.rows)
{
    csv += "\n";
    csv += row.FooName + ",";
    csv += row.FooMoney + ",";
}

System.IO.File.WriteAllText(filePath + fileName, csv);

这就是有趣的。调试时,row.FooMoney(十进制类型)可能是10000.在写入的csv中它是10,000,当然,我不想要逗号是我的分隔符。

我已经尝试过row.FooMoney.ToString(“F”/“G”)无效......它似乎是在文件写入期间格式化的。是什么赋予了?这是故意的吗?

更新


以下是与该问题相关的所有代码:

public ActionResult PassJSON(string __obj)
{
    var filePath = Url.Content("~/Temp/");
    var fileName = "Report.csv";

    try
    {
        string csv = string.Empty;
        var records = JsonConvert.DeserializeObject<List<RootReportObject>>(__obj);
        csv += "Date Created,Ref #,Name,Vehicle #,Address,ProductID,Product,Weight1,Weight2,";

        foreach (var record in records)
        {
            var count = record.count;
            var value = record.title;

            csv += "\n" + value + ",,,,,,,,,";

            foreach (var row in record.rows)
            {
                csv += "\n";
                csv += row.DateCreated + ",";
                csv += row.RefNo + ",";
                csv += row.Name + ",";
                csv += row.VehicleNo + ",";
                csv += row.Address1 + ",";
                csv += row.ProductID + ",";
                csv += row.Product + ",";
                csv += row.Weight1.ToString(CultureInfo.InvariantCulture) + ",";
                csv += row.Weight2.ToString(CultureInfo.InvariantCulture) + ",";
            }
        }

        System.IO.File.WriteAllText(filePath + fileName, csv);

        return Json(filePath + fileName, JsonRequestBehavior.AllowGet);
    }
    catch (Exception e)
    {
        return Json(e, JsonRequestBehavior.AllowGet);
    }
}

[Serializable]
public class Row
{
    public int id { get; set; }
    public DateTime DateCreated { get; set; }
    public int RefNo { get; set; }
    public string Name { get; set; }
    public string VehicleNo { get; set; }
    public string Address1 { get; set; }
    public int? ProductID { get; set; }
    public string Product { get; set; }
    public Decimal Weight1 { get; set; }
    public Decimal Weight2 { get; set; }
}

[Serializable]
public class RootReportObject
{
    public bool __group { get; set; }
    public int level { get; set; }
    public int count { get; set; }
    public string value { get; set; }
    public string title { get; set; }
    public int collapsed { get; set; }
    public List<Row> rows { get; set; }
    public object groups { get; set; }
    public string groupingKey { get; set; }
}

示例输出

8/16/2013 2:31:16 PM,72,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:31:34 PM,73,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,5,000,0,
8/16/2013 2:32:12 PM,74,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:33:15 PM,75,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/16/2013 2:50:58 PM,76,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/20/2013 7:19:32 PM,77,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/20/2013 7:46:03 PM,78,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,40,
8/20/2013 7:55:56 PM,79,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,1,
8/20/2013 8:13:58 PM,80,John Doe,,9021 Beverly Hills Blvd,427,Corn,0,50,
8/21/2013 8:05:25 PM,81,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,
8/22/2013 7:33:03 PM,82,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,50,
8/22/2013 7:40:42 PM,83,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,
8/22/2013 8:05:25 PM,84,John Doe,LP1234,9021 Beverly Hills Blvd,427,Corn,0,5,000,

更新#2


我删除了原始的report.csv,发现下次编写报告的调用从IIS中产生了404错误。显然WriteAllText()不再写了。不幸的是,我不知道该怎么办。我已经修改了代码,试图在写入发生之前删除任何以前的版本,但仍然没有得到任何东西

修改代码

if (System.IO.File.Exists(filePath + fileName))
  System.IO.File.Delete(filePath + fileName);

System.IO.File.WriteAllText(filePath + fileName, csv);
return Json(filePath + fileName, JsonRequestBehavior.AllowGet);

2 个答案:

答案 0 :(得分:4)

使用row.FooMoney.ToString("f")可以防止写入数千个分隔符。

如果您使用的是覆盖默认行为的奇怪文化设置,则可以通过row.FooMoney.ToString("f", CultureInfo.InvariantCulture)强制执行此操作。

请注意,如果您使用的是.NET 4或更高版本,则可以通过以下方式提高效率:

System.IO.File.WriteAllLines(System.IO.Path.Combine(filePath, fileName),
   record.Rows.Select(row => row.FooName + "," + row.FooMoney.ToString("f")));

答案 1 :(得分:1)

您的filePath变量将包含相对于您的Web应用程序的路径,而不是您的文件系统。 WriteAllText需要文件系统上的路径。

要获取物理路径,您可以执行Server.MapPath("~/Temp"),这将返回c:\websites\mysite\Temp或其他任何内容。从那里,您应该使用Path.Combine(filePath, fileName)获取完整的文件路径并将文件保存到该位置。