- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不明白为什么会这样,但确实如此。我正在写一个这样的 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,
我删除了原始的 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);
最佳答案
使用 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")));
关于c# - 为什么 File.WriteAllText() 通过添加逗号来格式化我的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938403/
File.WriteAllText 在每个字母和引号后插入一个空格。 例子: 原始文件 "JobID" "ParentJobID" 新建文件 " J o b I D " " P a r e n
我正在尝试将许多不同的表单保存到一个文本文件中,目前我有: 温度 风 下雨 云 我遇到的问题是它只保存“云”数据。有没有一种方法可以保存提交时的所有信息,并用逗号、破折号或空格将它们分隔开? 这是我目
我现在收到 3 份用户机器在使用我的软件时崩溃的报告。崩溃与我的程序无关,但当他们重新启动配置文件时,我的程序写入的所有文件都已损坏。 文件的写入方式没有什么特别之处,只需创建一个 Json 表示并使
假设我正在使用 File.WriteAllText 将一个很长的字符串写入一个文件,而另一个线程或进程正在尝试读取同一个文件。它会抛出任何异常吗?换句话说,File.WriteAllText 方法使用
这段代码的输出 string fileDateTime = "StepsGA-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".txt"; st
这个问题已经有答案了: File.WriteAllText not flushing data to disk (1 个回答) 已关闭 5 年前。 我有以下代码来保存数据: File.Copy("da
我有一个问题显然是 been encountered before ,尽管解决方案对我所看到的没有帮助。 我正在尝试根据 Dictionary 的内容的 base 64 编码将数据写入文件.它应该是这
我不明白为什么会这样,但确实如此。我正在写一个这样的 csv 文件: foreach (var row in record.rows) { csv += "\n"; csv += ro
我正在构建一个必须将用户输入写入文件的简单 C# 应用程序。 我的第一个问题是我希望用户能够设置设置文件的路径,但我希望程序能够在以下启动时读取设置文件。 我的第二个问题是,如果我使用类似的东西:
我有很多文件,我想随机写入一个文件,我想做的是: StringBuilder sb = new Stringbuilder(); int x; Random rnd = new Random();
对远程路径执行 File.WriteAllText 会抛出 UnauthorizedAccessException。当我在记事本中打开文件时,我可以毫无问题地对其进行编辑。试图修改文件的进程以我自己的
我有一些代码可以将字符串保存到文件中,如下所示: string TheFileJS = HttpRuntime.AppDomainAppPath + "\\SomePath\\" + ClientFi
文档说: // Summary: // Creates a new file, writes the specified string to the file, and then closes
这个问题在这里已经有了答案: File being used by another process after using File.Create() (11 个答案) 关闭 9 年前。 当我编写如
这一定是我见过的最奇怪的异常之一。使用File.WriteAllText静态方法写入文件时,不能写入特定文件名:“PRN”。你可以用一个简单的衬垫重新创建它: File.WriteAllText("c
我有一个非常简单的 C# 程序,它迭代多个文件并替换所有文件中的字符串。 但是,当我使用 Git 比较这些文件时,它突出显示了我所有文件的更改。 我的 C# 代码是: string[] files =
我注意到我使用以下代码创建的文件中没有换行符。在我还存储文本的数据库中,这些都存在。 string story = "Critical error occurred after " + elaps
我正在使用 File.WriteAllText 创建一个文件,并使用 File.Copy 方法将同一文件复制到另一个目录。但出于某种原因,它没有在源目录中创建文件,而是将其复制到目标目录。 可能是什么
我有一些原始数据 (xml),我收到的肯定包含 unicode。我使用以下方法将它们写入文件: File.WriteAllText 这似乎删除/更改了 unicode 字符。有办法避免这种情况吗? 最
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
我是一名优秀的程序员,十分优秀!