3

我不明白为什么会发生这种情况,但确实如此。我正在写一个这样的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);
4

2 回答 2

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")));
于 2013-11-12T19:46:34.420 回答
1

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

To get the physical path, you would do Server.MapPath("~/Temp"), which will return c:\websites\mysite\Temp or whatever. From there, you should use Path.Combine(filePath, fileName) to get your full file path and save the file to that location.

于 2013-11-12T21:24:47.837 回答